Brief introduction to PHP namespace and its application in automatic loading

Source: Internet
Author: User
Brief introduction to the namespace of PHP and its Application for Automatic loading php introduced namespace since 5.3, which makes php more standardized and clear in the process of object-oriented design, at the same time, the automatic loading mode in the framework architecture is fully used to improve the performance of the framework itself-load on demand

No namespace declaration is actually under the root namespace

 '; Echo _ CLASS __.'
';}} // If it starts with' \ ', the class new Web () in the current namespace is used (); // of course, we can point out that the complete class name \ Project \ Web is the Web class new \ Project \ Web () in the Project namespace under the root namespace;?>

1. if no namespace declaration exists, it is under root namespace '\' by default.

2. if there is a namespace life, the default class is instantiated on this namespace as the premise, unless you use the use statement to declare a complete class name.

For example

 

In fact, php's use statement does not import this class to the current work area, but simply specifies

use Web\Tools;

The tool class name is Web \ Tools

When you are new, the system checks whether this class exists in the current workspace. if not, it checks whether the function is automatically loaded.

If no, an error is returned. Otherwise, your auto-load function will be called.

Pass the complete class name you want to instantiate

New Tools (); // The class named Web \ Tools will be instantiated. if the class is not included and some functions are automatically loaded, Web \ Tools will be passed to the next function, you can use the passed class name for your own import.

Most frameworks map paths based on class names.

For example, Yii Framework

// The namespace app \ Controllers of the current namespace; // declare that the Controller is in the yii \ web namespace. if it is useful to the Controller, the complete name is yii \ web \ Controlleruse yii \ web \ Controller; // This class is also required for inheritance, therefore, the current working directory will require you to check whether there are yii \ web \ Controller classes. Unfortunately, no //, but the system will give you almost one time to see if there are auto-loaded functions, yii, of course, is passed to the loading function 'yii \ web \ controller' as the parameter // The loading function resolution class name. if the class name is found to start with yii, path ING is performed, map yii \ web \ Controller to the path of its Framework Class Library File // that is, vendor \ yiisoft \ yii2, and splice it with web \ Controller. php suffix. the namespace of the class saved in the file // is yii \ web, and the class name is Controllerclass IndexController extends Controller {}

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.