Home >Backend Development >PHP Tutorial >[namespace]Basics of using PHP namespace, namespace namespace_PHP tutorial
---------------------- -------------------------------------------------- --------------------------
1. PHP’s namespace mainly solves three conflict problems: constants, functions, and classes
Popular understanding: namespace is equivalent to building a directory, and placing the code under the namespace in the directory to distinguish it from the outside.
<span>/*</span><span> |--------------------------------- |namespace示例 |@黑眼诗人 <www.chenwei.ws> |--------------------------------- </span><span>*/</span><span> namespace myself; </span><span>function</span> <span>var_dump</span><span>() { </span><span>echo</span> 100<span>; } </span><span>var_dump</span>(100); <span>//</span><span>调用自定义函数(相对路径方式)</span> <span> \myself\</span><span>var_dump</span>(100); <span>//</span><span>调用自定义函数(绝对路径方式) </span> <span> \</span><span>var_dump</span>(100); <span>//</span><span>调用全局的(系统的函数)</span>
Note: There cannot be any code before namespace, except declare(); multiple files can use the same namespace, but the content defined in the same namespace cannot conflict. Namespace supports sub-namespaces, such as namespace myselfgood, which is equivalent to the concept of multi-level directories.
2. Multiple namespaces exist in the same file
1.
<span>/**<br /> * chenwei.ws<br /> */<br />namespace nihao\shijie; namespace hello\world; </span><span>function</span><span> test() { </span><span>//</span><span>........</span> <span>} </span><span>//</span><span>连续命名,后面的代码将使用第二个命名空间,所有连续命名没有意义。</span>
2.
<span>/*</span><span>* * 同一文件中若使用了多个命名空间,建议大括号扩起来 </span><span>*/</span><span> namespace nihao\shijie{ </span><span>function</span><span> test_one()<br /> { </span><span>//</span><span>......</span> <span> }; } namespace hello\world{ </span><span>function</span><span> test_two()<br /> { </span><span>//</span><span>........</span> <span> } } \nihao\shijie\test_one(); \hello\world\test_two();</span>
Using multiple namespaces in the same file is mainly used for projects to merge multiple PHP scripts into the same file. In practice, its use is not recommended!
3. Name resolution rules (several concepts)
1. Unqualified name: The name does not contain the namespace separator, such as: myself
2. Qualified name: The name contains a namespace delimiter, such as: nihaoshijie
3. Fully qualified name: The name contains a delimiter and starts with a namespace delimiter, such as: nihaoshijie (that is, the concept of absolute path)
-------------------------------------------------- --------------------------------------------------
1. namespace Zend\Http\PhpEnvironment;
This code defines a namespace, which you can understand as defining a domain name named Zend\Http\PhpEnvironment.
After definition, the classes, interfaces, const, etc. declared below are all in the declared "domain". When referencing an include file that declares a namespace, and if you want to call something in it, you must:
Adjust the current script to this domain name, otherwise, you have to use the full name of namesapce.
For example, inc.php file:
namespace Zend\Http\PhpEnvironment;
class Bar {}//define a class
when called by other files :
// The first way to access Foo, use the full name
require 'inc.php';
$foo = new \Zend\Http\PhpEnvironment\Bar();
//The second method of accessing Foo
namespace Foo; //Adjust the current script to the ns domain of Foo, and the namespace declaration must be in the first sentence
require 'inc.php';
$foo = new Bar();
2. The purpose of the use keyword is to use the alias of ns:
For example, the above
// is the first to access Foo This method uses the full name
require 'inc.php';
$foo = new \Zend\Http\PhpEnvironment\Bar();
After using uses, the writing is as follows:
use \Zend\Http\PhpEnvironment as pe; //Define alias
$foo = new \pe\Bar(); //Use short alias to replace original
if Omit the following as...., then you can directly replace it with the text of the last section, for example, the above:
use \Zend\Http\PhpEnvironment; //Define alias
$ foo = new \PhpEnvironment\Bar(); //Replace the original
======================== with a short alias ========================
Relevant content in the official PHP manual:
In PHP, namespace naming Space is used to solve two types of problems encountered when creating reusable code such as classes or functions when writing class libraries or applications:
1. User-written code and PHP internal classes/functions/constants Or name conflicts between third-party classes/functions/constants.
2. Create an alias (or short) name for a very long identifier name (usually defined to alleviate the first type of problem) to improve the readability of the source code.
PHP namespaces provide a way to group related classes, functions, and constants together.
PHP namespace supports two ways of using aliases or imports: using aliases for class names, or using aliases for namespace names. Aliases are implemented through the operator use. ...The rest of the text>>
No problem with grammar.
What is your PHP version? PHP supports namespaces starting from version 5.3.0. Your PHP version may be lower.