PHP namespace/category name use/framework automatic loading mechanism

Source: Internet
Author: User
PHP namespace Category name use framework automatic loading mechanism from php 5.3, PHP has many main features such as namespace/latency static binding/lambda anonymous functions

Namespace references further improve the flexibility and controllability of the php code structure, and facilitate the classification and management of various tool classes of the project.

Various php frameworks use namespaces for file path ING and automatic loading.

Next we will take Yii2 for an analysis.

Let's take a look at the Yii2 controller writing rules.

Controllers \ IndexController. php

 

1. the complete name of a class in a namespace is namespace \ className. when you access a class in another namespace in a namespace context, you can use for alias, you can also use the complete name of this class, but it must start with '\' root namespace. Otherwise, the interpreter will think that you are calling it in the context of the current namespace, that is, the foo \ bar Method is loaded as currentNamespace \ foo \ bar.

The namespace is very similar to the linux file system. '\' indicates the root. if it does not start with the root, it is considered as the base point of the current namespace.

2. use only defines the short alias for the class you are using. after use foo \ bar, the new bar () is new \ foo \ bar (). There is also a trick, when we reference a class with the same name in different namespaces, we can use as to define a new alias for it.

use foo\bar\sameName as classA;use bar\foo\sameName as classB;new classA(); // new \foo\bar\sameName;new classB(); // new \bar\foo\sameName;

3. when we use the entry file to load parameters to instantiate an application subject to load the routing component and request the dispatch controller to call the method, other classes will be called during this period, such

use yii\web\Controller;

The system will perform the most loading attempt through the automatic loading function. if this type still cannot be loaded, an error is returned.

Next, let's take a look at the process of Yii2 from the entry file to register an Application Entity and automatically load the function.

Index. php

The entry file will be called using require when loading the configuration and system framework, because no automatic loading function has been registered.

The automatic loading function is registered through spl_autoload_register when the Yii bootstrap file is loaded.

Yii. php

Automatic loading function of Yii2

Inherit to BaseYii. what it needs to do is map the file path of this class based on the class name of your namespace type.

For example, the yii \ web \ Controller class maps to YII2_PATH based on yii. '/web/Controller. php file, and this file is the Controller class of the namespace yii \ web. load this file to access the yii \ web \ Controller class.

However, the controller or model we write ourselves is 'app \ controllers \ indexcontroller' during access, 'app \ models \ EntryForm'

The autoload function locates the ing keyword in the controllers or models folder to read the corresponding file and load the corresponding class, this is also why the class name and file name correspond to each other. if there is no correspondence, you can only load the class you wrote in it through a fixed require file.

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.