Introduction to "namespaces" in new features of PHP5.3

Source: Internet
Author: User
New features of PHP5.3> <LINKhref = "http://www.php100.com//statics/style/headfloor_950_081205.css" type = text/cssrel = stylesheet> <LINKhref = "http://www.php100.com//statics

A new important feature of PHP 5.3 is namespace ).
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, 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.

$ User = new Project: Module: User (); $ user-> register ($ register_info); it is indeed the same as normal, however, we can associate two independent classes. For example

Project: Module: User; Project: Module: Blog; this makes it easier to describe and understand the relationship between variables and classes from the language itself, this avoids the lengthy naming method like Project_Module_Blog in the traditional mode.

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.

Use Project: Module; $ user = new Module: User (); $ user-> register ($ register_info); or even

Use Project: Module: User as ModuleUser; $ user = new ModuleUser; $ user-> register ($ register_info); constants in the class can also be accessed through namespaces, for example, STATUS_ OK in the above class can be passed through the namespace

Project: Module: User: STATUS_ OK. Further, you can use aliases to simplify the long "variable name"

Use Project: Module: User: STATUS_ OK as STATUS_ OK; echo STATUS_ OK; by The way, The concept of "The Global Namespace" is introduced. The so-called "hyperspace" means that no NAMESPACE variables, classes, and functions are specified. For example

Function foo () {...} 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:

Function _ autoload ($ classname) {$ classname = strtolower ($ classname); $ classname = str_replace (':', DIRECTORY_SEPARATOR, $ classname ); require_once (dirname (_ FILE __). '/'. $ classname. '. class. php ');}, such as calling

_ Autoload ('Project: Module: user'); you can automatically load the Project_Module_User.class.php File (although this does not seem convenient ).

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.