Home >Backend Development >PHP Tutorial >php3 that those tutorials don't have
php.net
(PHP 5 >= 5.3.0, PHP 7)
Defining namespaces
While any legal PHP code can be included in a namespace, only the following types of code are affected by namespaces, They are: classes (including abstract classes and traits), interfaces, functions and constants.
If a file contains a namespace, it must declare the namespace before all other code except one: the declare keyword. All non-PHP code, including whitespace, must not appear before a namespace declaration.
<html> <?phpnamespace MyProject; // 致命错误 - 命名空间必须是程序脚本的第一条语句 ?>
The same namespace can be defined in multiple files, which allows the contents of the same namespace to be divided and stored in different files.
Define sub-namespaces
<?php namespace MyProject\Sub\Level; const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } ?>
Define multiple namespaces in the same file
Note: In actual programming practice, it is highly discouraged to define multiple namespaces in the same file!
<?php namespace MyProject { const CONNECT_OK = 1; class Connection { /* ... */ } function connect() { /* ... */ } }namespace AnotherProject {const CONNECT_OK = 1;class Connection { /* ... */ }function connect() { /* ... */ } }?>
Using namespaces
Three ways to refer to class names:
Unqualified names (identifiers that do not contain namespace separators in their names), or class names that do not contain a prefix. For example $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 (identifiers with namespace delimiters in their names), or names that include a prefix, 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 (an identifier whose name contains a namespace delimiter and starts with the namespace delimiter), or a name that includes a global prefix operator. To access any global class, function, or constant, use a fully qualified name, such as strlen() or Exception .
tips:
$a = '\namespacename\classname'; $obj = new $a; $a = 'namespacename\classname'; $obj = new $a; $b = 'namespacename\funcname'; $b(); // prints namespacename\funcname$b = '\namespacename\funcname'; $b(); // also prints namespacename\funcname
Use related constants
The value of the constant __NAMESPACE__ is a string containing the name of the current namespace. In global code, not included in any namespace, it contains an empty string.
Use namespace:alias/import
<?php namespace foo; use My\Full\Classname as Another; // 下面的例子与 use My\Full\NSname as NSname 相同 use My\Full\NSname; // 导入一个全局类 use ArrayObject; // importing a function (PHP 5.6+) use function My\Full\functionName; // aliasing a function (PHP 5.6+) use function My\Full\functionName as func; // importing a constant (PHP 5.6+) use const My\Full\CONSTANT; $obj = new namespace\Another; // 实例化 foo\Another 对象 $obj = new Another; // 实例化 My\Full\Classname 对象 NSname\subns\func(); // 调用函数 My\Full\NSname\subns\func $a = new ArrayObject(array(1)); // 实例化 ArrayObject 对象 // 如果不使用 "use \ArrayObject" ,则实例化一个 foo\ArrayObject 对象 func(); // calls function My\Full\functionName echo CONSTANT; // echoes the value of My\Full\CONSTANT ?>
Class names always resolve to names in the current namespace. Therefore when accessing a class name that is internal to the system or not contained in a namespace, you must use the fully qualified name.