use of namespaces becomes crucial when application code grows. to give a unique name to each class/function may become tedious and not exactly elegant, namespace comes handy. for example, if we need to declare a calculate() function to calculate area as well as tax, instead of defining them as something like calculate_area() and calculate_tax(), we can create two namespaces area and tax and use calculate() inside them.
use of namespaces solves two problems.
avoiding name collisions between classes/functions/constants defined by someone with third-party classes/functions/constants.
provides ability to alias (or shorten) extra_long_names thereby improving readability of source code.
php namespaces provide a way in which to group related classes, interfaces, functions and constants. namespace names are case - insensitive
example<?phpnamespace myspace;function hello() { echo "hello world";}?>
to call a function defined inside a namespace, include with use keyword. name of function is qualified with namespace
example live demo
<?phpnamespace myspace;function hello() { echo "hello world";}use myspace;myspace\hello();?>
输出上述代码现在返回以下输出的名称
hello world
以上就是php命名空间概述的详细内容。
