PHP 5.3 from php more namespace/delay static binding/lambda anonymous functions and other key features
Namespace references further enhance the flexibility and controllability of PHP's code structure, facilitating the collation and management of the project's various tool classes
Each PHP framework is a function that uses namespaces to automatically load file path mappings.
Let's take Yii2 for an analysis.
Let's take a look at YII2 's controller writing rules.
controllers\indexcontroller.php
1. The full name of a class under a namespace is namespace\classname, and when a class is accessed under another namespace in the context of a namespace, we can use it to alias, or use the full name of the class, but start with the ' \ ' root namespace. Otherwise the interpreter will assume that you are calling in the context of the current namespace, that is, the Foo\bar method will be loaded in Currentnamespace\foo\bar way
The namespace is similar to the Linux file system, where ' \ ' stands for the root, and does not start with the root as a base point for the current namespace
2, use just give you the class definition of the short alias, using Foo\bar after the new bar () is new \foo\bar (), there is a small trick, when we reference the same class name under different namespaces in the same class, you can use as to define a new alias
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 instantiate an application principal load Routing component resolution request dispatch Controller call method through the Portal file load parameter configuration, other classes are called during the process, such as
Use Yii\web\controller;
The system will go through the automatic loading function to make the most attempt to load, if it still does not load the class error
Let's take a look at the YII2 the process of registering an auto-load function after starting an application entity from the portal file
index.php
The Require call is used when the portal file loads the configuration and system framework, because the auto-load function is not yet registered
Automatic load function registered via Spl_autoload_register when loading Yii bootstrap file
yii.php
YII2 Auto-loading function
Inherit to Baseyii it's going to be mapped to the file path where the class resides based on the class name of your namespace type.
For example, the Yii\web\controller class will map to Yii2_path based on Yii. '/web/controller.php ' file, and this file is the Controller class with the namespace yii\web, loading the file to access the Yii\web\controller class
And our own controller or model is accessed as ' App\controllers\indexcontroller ' app\models\entryform '
The AutoLoad function will be able to load the corresponding class by locating it under the controllers or models folder according to the app for the Mapping keyword, which is why the class name and file name correspond to each other, and if there is no correspondence, you can only pass the fixed re Quire a file to load the class you wrote in.