Home  >  Article  >  Backend Development  >  Introduction to namespaces, new features of PHP5.3_PHP Tutorial

Introduction to namespaces, new features of PHP5.3_PHP Tutorial

WBOY
WBOYOriginal
2016-07-13 17:36:11798browse

An important new feature of PHP 5.3 is namespace.
This feature was proposed in PHP5.0x, but was later canceled and scheduled to be implemented in PHP6. And this time, PHP5.3 was released "ahead of schedule" again, which shows that developers attach great importance to it and are cautious.

The content of the official release may be out of date (documentation maybe out dated), so here is a brief explanation of the usage of namespace: First, declare a namespace and add the new keyword namespace, which should be in the class Beginning of file

$user = new Project::Module::User();$user->register($register_info); is indeed the same as usual, but we can connect 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, thus avoiding the lengthy "traditional" Project_Module_Blog Naming method.

The above description may be difficult to explain the benefits of using namespaces. The newly added use and as keywords may explain the problem better. Use and as statements can reference and declare namespace "aliases". For example, the code for instantiating the class in the above controller can be written like this

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 the namespace, such as STATUS_OK in the above class. via namespace

Project::Module::User::STATUS_OK access. Furthermore, you can also use aliases to simplify such long "variable names"

use Project::Module::User::STATUS_OK as STATUS_OK;echo STATUS_OK;By the way, mention the concept of "The Global Namespace". The so-called "hyperspace" refers to variables, classes and functions that do not have a designated namespace. For example

function foo() {...} can be executed using foo() or ::foo();.

Finally, use the autoload function to load the class in the specified namespace. The simple function 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

__autoload(Project::Module::User); can automatically load the Project_Module_User.class.php file (although this seems inconvenient).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/508245.htmlTechArticleA new important feature of PHP 5.3 is namespace. This feature was proposed in PHP5.0x, but was later canceled and scheduled to be implemented in PHP6. And this time again "...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn