Simple PHP Namespace tutorial. This feature was mentioned in PHP5.0x and was later canceled and scheduled to be implemented in PHP6. The release of PHP5.3 was "in advance" again this time. it can be seen that the developers' attention to it and this feature were mentioned at PHP5.0x, and was later canceled and scheduled to be implemented in PHP6. The release of PHP5.3 was "in advance" again, which shows the importance and cautious attitude of developers.
When officially released, the document content may have expired (documentation maybe out dated), so here we will briefly describe the usage of the namespace: first, declare a namespace, A new keyword namespace should be added at the beginning of the class file.
The code is as follows:
Namespace Project: Module;
Class User {
Const STATUS_ OK = true;
Function register ($ data ){
...
}
...
}?>
Then you can call
The code is as follows:
$ User = new Project: Module: User ();
$ User-> register ($ register_info );
It is indeed no different from the ordinary one, but we can associate two classes that are independent of each other. For example
The code is as follows:
Project: Module: User;
Project: Module: Blog;
In this way, it is easier to describe and understand the relationship between variables and classes from the language itself, thus avoiding the lengthy naming method such as Project_Module_Blog in the traditional way.
The above description may be difficult to explain the benefits of using a namespace. the newly added use and as keywords may better illustrate the problem. The use and as statements can reference and declare the "Alias" of a namespace ". For example, the code of the instantiated class in the controller can be written in this way.
The code is as follows:
Use Project: Module;
$ User = new Module: User ();
$ User-> register ($ register_info );
Even
The code is as follows:
Use Project: Module: User as ModuleUser;
$ User = new ModuleUser;
$ User-> register ($ register_info );
Constants in the class can also be accessed through the namespace. for example, STATUS_ OK in the above class can be accessed through the namespace
The code is as follows:
Project: Module: User: STATUS_ OK
Access. Further, you can use aliases to simplify the long "variable name"
The code is as follows:
Use Project: Module: User: STATUS_ OK as STATUS_ OK;
Echo STATUS_ OK;
By The way, The concept of "The Global Namespace" is mentioned. The so-called "hyperspace" means that no NAMESPACE variables, classes, and functions are specified. For example
The code is as follows:
Function foo (){
...
}
This function can be executed using foo () or: foo.
Finally, use the autoload function to load the class of the specified namespace. Simple functions are as follows:
The code is as follows:
Function _ autoload ($ classname ){
$ Classname = strtolower ($ classname );
$ Classname = str_replace (':', DIRECTORY_SEPARATOR, $ classname );
Require_once (dirname (_ FILE _). '/'. $ classname. '. class. php ');
}
In this way, for example, calling
The code is as follows:
_ Autoload ('Project: Module: user ');
You can automatically load the Project_Module_User.class.php File (although it does not seem convenient ).
PHP5.0x was proposed, and was canceled and scheduled to be implemented in PHP6. The release of PHP5.3 was "in advance" again this time. it can be seen that developers pay attention to it and...