PHP Namespace (Namespace) Concise Tutorial

  • 2021-06-29 10:32:58
  • OfStack

This feature was introduced at PHP5.0x and was later cancelled and scheduled for implementation in PHP6.This time, the release of PHP5.3 was "ahead of schedule" again, which shows the importance and caution of developers.

The content of the documentation may be out of date when it is officially published (documentation maybe out dated), so here's a simple explanation of the use of namespaces: first, declare a namespace, add a new keyword namespace, which should be at the beginning of the class file


<?php   
namespace Project::Module;    
class User {       
    const STATUS_OK = true;       
    function register($data) {   
        ...       
    }   
    ...   
}?>


This can then be called in the controller (possibly another file)


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

It's true that it's not different from normal, but we can connect two separate classes.such as


Project::Module::User;   
Project::Module::Blog;

This makes it easier to describe and understand the relationships between variables and classes from the language itself, avoiding Project_on the "traditional"sideModule_Blog is such a lengthy way of naming.

The above instructions may be difficult to explain what the benefits of using namespaces are, and the new use and as keywords may explain the problem better.The use and as statements can reference and declare the "alias" of a namespace.For example, the code for instantiating a class in the controller above can be written as follows


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

Even to the extent that


use Project::Module::User as ModuleUser;   
$user = new ModuleUser;   
$user->register($register_info); 

Constants in classes can also be accessed through namespaces, such as STATUS_in the above classOK can use namespaces


Project::Module::User::STATUS_OK

Visit.One step forward, you can also use aliases to simplify variable names of that length


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

Incidentally, the concept of "hyperspace" (The Global Namespace) is mentioned.A hyperspace is a variable, class, and function that does not have a namespace specified.such as


function foo() {   
    ...   
}

This function can be executed with foo(), or with the following functions::foo();This is done.

Finally, classes with the specified namespace can be loaded using the autoload function together.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 Project_Module_The User.class.php file (although this may not seem convenient).


Related articles: