Before discussing how to use namespaces, you must understand how PHP knows which namespace elements to use. A simple analogy can be made between the PHP namespace and the file system. There are three ways to access a file in the file system:
Relative file name format such as foo.txt. It will be parsed as currentdirectory/foo.txt, where currentdirectory represents the current directory. So if the current directory is /home/foo, the file name is resolved to /home/foo/foo.txt.
The relative path name format is such as subdirectory/foo.txt. It will be parsed as currentdirectory/subdirectory/foo.txt.
The absolute path name is in the form of /main/foo.txt. It will be parsed as /main/foo.txt.
Elements in the PHP namespace use the same principle. For example, a class name can be referenced in three ways:
an unqualified name, or a class name that does not include a prefix, such as $a=new foo(); or foo::staticmethod();. If the current namespace is currentnamespace, foo will be resolved to currentnamespacefoo. If the code using foo is global and does not contain code in any namespace, foo will be resolved as foo. Warning: If a function or constant in the namespace is undefined, the unqualified function or constant name will be resolved to a global function or constant name.
Qualified names, or names containing prefixes, such as $a = new subnamespacefoo(); or subnamespacefoo::staticmethod();. If the current namespace is currentnamespace, foo will be resolved to currentnamespacesubnamespacefoo. If the code using foo is global, code not contained in any namespace, foo will be resolved to subnamespacefoo.
Fully qualified name, or a name that includes a global prefix operator, for example, $a = new currentnamespacefoo(); or currentnamespacefoo::staticmethod();. In this case, foo is always resolved to the literal name currentnamespacefoo in the code.
The following is an example of using these three methods:
file1.php:
<?php namespace Foo\Bar\subnamespace; const FOO = 1; function foo() {} class foo { static function staticmethod() {} } ?>
file2.php:
<?php namespace Foo\Bar; include 'file1.php'; const FOO = 2; function foo() {} class foo { static function staticmethod() {} } /* 非限定名称 */ foo(); // 解析为 Foo\Bar\foo resolves to function Foo\Bar\foo foo::staticmethod(); // 解析为类 Foo\Bar\foo的静态方法staticmethod。 echo FOO; // resolves to constant Foo\Bar\FOO /* 限定名称 */ subnamespace\foo(); // 解析为函数 Foo\Bar\subnamespace\foo subnamespace\foo::staticmethod(); // 解析为类 Foo\Bar\subnamespace\foo,以及类的方法 staticmethod echo subnamespace\FOO; // 解析为常量 Foo\Bar\subnamespace\FOO /* 完全限定名称 */ \Foo\Bar\foo(); // 解析为函数 Foo\Bar\foo \Foo\Bar\foo::staticmethod(); // 解析为类 Foo\Bar\foo, 以及类的方法 staticmethod echo \Foo\Bar\FOO; // 解析为常量 Foo\Bar\FOO ?>
Note that to access any global class, function or constant, you can use a fully qualified name, such as strlen() Or Exception or INI_ALL.
Example #1 Access global classes, functions and constants inside a namespace
<?php namespace Foo; function strlen() {} const INI_ALL = 3; class Exception {} $a = \strlen('hi'); // 调用全局函数strlen $b = \INI_ALL; // 访问全局常量 INI_ALL $c = new \Exception('error'); // 实例化全局类 Exception ?>