A new and important feature of PHP 5.3 is the namespace (namespace).
This feature was presented at php5.0x Time and was subsequently canceled and arranged for implementation in PHP6. This time again "advance" to the PHP 5.3 release, visible developers to its attention and cautious attitude.
The content of the document may have expired (documentation maybe out dated) at the time of the official release, so here is a simple description of the use of namespaces: First, declare a namespace, add a new keyword namespace, which should be at the beginning of the class file
12345678 |
<?php namespace Project::Moduletruefunction Register($data?> |
You can then call it in the controller (possibly other files)
12 |
New Project::Module::User()$user->Register($register _info ); |
It's true and normal, but we can relate two separate classes. Like what
12 |
Project::Module::User; Project::Module::Blog; |
This makes it easier to describe and understand the relationships between variables and classes from the language itself, thus avoiding the lengthy naming of Project_module_blog on the "traditional" approach.
The instructions above may be difficult to illustrate the benefits of using namespaces, and the newly added use and as keywords may be a better illustration of the problem. The use and as statements can reference and declare "aliases" of namespaces. For example, the code that instantiates the class in the above controller can be written like this
123 |
Use Project::modulenew module::User()$user->Register($ Register_info); |
Even
123 |
Use Project::Module:: Asmoduleusernew Moduleuser$user->Register($ Register_info); |
Constants in a class can also be accessed through namespaces, such as the STATUS_OK in the above class can be passed through namespaces
1 |
Project::Module::User::status_ok |
Access. Further, you can also use aliases to simplify the long "variable name"
12 |
Use Project::Module::User:: Asstatus_okecho status_ok; |
By the way, the concept of hyperspace (the Global Namespace) is mentioned. The so-called "hyperspace" is the variables, classes, and functions that do not specify namespaces. Like what
This function can be performed using foo (), or you can use:: foo (); Executed in this way.
Finally, you can load the class with the specified namespace with the AutoLoad function. The simple functions are as follows
12345 |
function __autoloadstrtolower)str_replace':: ', Directory_ SEPARATOR)require_oncedirname)} |
In this way, such as calling
1 |
__autoload(' Project::module::user '); |
You can automatically load Project_Module_User.class.php files