PHP Namespaces (Namespace) Concise tutorial _php Tutorial

Source: Internet
Author: User
This feature was raised at the time of Php5.0x and was later canceled and implemented in PHP6. And this time again "advance" to the PHP5.3 release, it can be seen that the developer of its attention and cautious attitude.

The description of the document may be out of date (documentation maybe out dated), so here's a simple description of the namespace usage: first declaring a namespace, adding a new keyword namespace, which should be at the beginning of the class file

Copy the Code code as follows:
namespace Project::module;
Class User {
Const STATUS_OK = true;
function Register ($data) {
...
}
...
}?>

It can then be called in the controller (possibly other files)

Copy the Code code as follows:
$user = new Project::module::user ();
$user->register ($register _info);
It's certainly not the same as usual, but we can relate two separate classes. Like what

Copy the Code code as follows:
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 the "traditional" project_module_blog.

The above instructions may be difficult to explain what benefits are being used with namespaces, and the newly added use and as keywords may better illustrate the problem. Use and as statements can refer to and declare "aliases" of namespaces. For example, the code for instantiating classes in the above controller can be written like this

Copy the Code code as follows:
Use Project::module;
$user = new Module::user ();
$user->register ($register _info);
Even

Copy the Code code as follows:
Use Project::module::user as Moduleuser;
$user = new Moduleuser;
$user->register ($register _info);
A constant in a class can also be accessed through a namespace, such as STATUS_OK in the above class, through a namespace

Copy the Code code as follows:
Project::module::user::status_ok
Access. Further, you can also use aliases to simplify so long "variable names"

Copy the Code code as follows:
Use PROJECT::MODULE::USER::STATUS_OK as STATUS_OK;
Echo STATUS_OK;
By the way, the concept of "hyperspace (the Global Namespace)" is mentioned. The so-called "hyperspace" is a variable, class, and function that does not specify a namespace. Like what

Copy the Code code as follows:
function foo () {
...
}
This function can be performed using Foo () and can also be used:: foo (); This executes.

Finally, you can load a class of the specified namespace with the AutoLoad function. The simple functions are as follows

Copy the Code code as follows:
function __autoload ($classname) {
$classname = Strtolower ($classname);
$classname = Str_replace (':: ', Directory_separator, $classname);
Require_once (DirName (__file__). '/' . $classname. '. class.php ');
}
This, for example, calls

Copy the Code code as follows:
__autoload (' Project::module::user ');
You can automatically load the Project_Module_User.class.php file (although this may not seem convenient).

http://www.bkjia.com/PHPjc/781812.html www.bkjia.com true http://www.bkjia.com/PHPjc/781812.html techarticle This feature was raised at the time of Php5.0x and was later canceled and implemented in PHP6. And this time again "ahead" to the PHP5.3 release, visible to developers of its attention and ...

  • 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.