PHP 5.3 Features: namespaces

Source: Internet
Author: User
Tags aliases

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

123      
function foo(} 

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.