PHP5.3 features: namespace _ PHP Tutorial

Source: Internet
Author: User
PHP5.3 features: namespace. A new important feature of PHP5.3 is namespace ). This feature was mentioned in PHP5.0x and was later canceled and scheduled to be implemented in PHP6. Another 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 PHP 5.3 was "in advance" again this time, showing 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.

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

Then you can call

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

It is indeed no different from the ordinary one, but we can associate two classes that are independent of each other. For example

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

In this way, it is easier to describe and understand the relationship between variables and classes from the language itself, thus avoiding the lengthy naming method such as Project_Module_Blog in the traditional way.
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.

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

Even

123      
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

1      
Project::Module::User::STATUS_OK

Access. Further, you can use aliases to simplify the long "variable name"

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

By The way, The concept of "The Global Namespace" is mentioned. The so-called "hyperspace" means that no NAMESPACE variables, classes, and functions are specified. For example

123      
function foo() {    ...       }

This function 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:

12345      
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

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

Automatically load the Project_Module_User.class.php file.

A new important feature of http://www.bkjia.com/PHPjc/371510.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/371510.htmlTechArticlePHP 5.3 is namespace ). This feature was mentioned in PHP5.0x and was later canceled and scheduled to be implemented in PHP6. Again, "...

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.