Home >Backend Development >PHP Tutorial >A brief tutorial on PHP namespace (Namespace)_PHP tutorial

A brief tutorial on PHP namespace (Namespace)_PHP tutorial

WBOY
WBOYOriginal
2016-07-13 10:28:34758browse

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 The beginning of the file

Copy the code The code is as follows:

namespace Project::Module ;                                                                                            🎜 >

Then in the controller (maybe other files) you can call




Copy the code

The code is as follows:


$user = new Project::Module::User();
$user->register($register_info);It is indeed the same as usual, but We can connect two independent classes. For example

Copy code
The code is as follows:

Project::Module::User;

Project::Module::Blog ;This makes it easier to describe and understand the relationship between variables and classes from the language itself, thereby avoiding the "traditional" lengthy naming method of Project_Module_Blog. 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


Copy code
The code is as follows:

use Project::Module;

$user = new Module::User( ); $user->register($register_info); Even


Copy code
The code is as follows:

use Project::Module::User as ModuleUser;

$user = new ModuleUser; $user->register($register_info); Constants in the class can also be accessed through the namespace. For example, STATUS_OK in the above class can be accessed through the namespace


Copy code
The code is as follows:

Project::Module::User::STATUS_OK

Access. Furthermore, you can also use aliases to simplify such long "variable names"
Copy code
The code is as follows:

use Project::Module::User::STATUS_OK as STATUS_OK;

echo STATUS_OK;By the way, let me mention the concept of "Hyperspace (The Global Namespace)". The so-called "hyperspace" refers to variables, classes and functions that do not have a designated namespace. For example

Copy code
The code is as follows:

function foo() {

... } This function 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



Copy code
The code is as follows:

function __autoload( $classname ) {                                                                                                                    ; ; 🎜>

In this way, for example, calling
Copy code

The code is as follows:


__autoload('Project::Module::User');

You can automatically load the Project_Module_User.class.php file (although this seems inconvenient).

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/781812.htmlTechArticleThis feature was proposed in PHP5.0x, but was later canceled and scheduled to be implemented in PHP6. And this time PHP5.3 has been released "ahead of schedule" again, which shows that developers attach great importance to it and...
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