Recently, a friend asked me if the Use keyword in PHP will be automatically introduced into the file? In fact, the USE keyword does not have anything to do with file loading, which is the function of declaring the namespace under which the class is to be used.
Objective
It may be that you often use the framework, write one Controller or Model the time, write a lot use , but did not write the file loading code, it is assumed that the use of the file can be automatically loaded.
Detailed Introduction
In fact, now the popular PHP framework is based on the MVC pattern, a lot of use of namespaces, to improve the flexibility of the program. So how does the framework implement the script file that is associated with the class library declared by the USE keyword?
(1): When declaring a use class library declaration through a keyword, it does not load the script, but only when the script file is actually used to the corresponding class library (this is called lazy loading).
(2): The framework is generally in the initialization stage, the declaration of the __ autoload() function or spl_autoload_register() function (typically declared in the portal file). When invoking a class library by using the use declaration, the above function is called automatically, passing the called Class library name to the function (where the class library name is the full name of the namespace)
<?phpnamespace home\controller;function __autoload ($class _name) { //echo $class _name. " <br/> "; Require_once ("./". $class _name. ". php"); } Use Admin\controller\index;new Index; The new index triggers the __autoload () function, returning Admin\controller\index to the function?>
(3): In the framework defined in the __ autoload() function or spl_autoload_register() function after receiving the passed class library name, the framework will process the name, parse out the class library name corresponding to the file path, and then the file loading. It is important to note that different frameworks in the parsing of the class library name, the way to parse the file path is inconsistent, after all, the respective directory structure is not the same.
For example, thinkphp:
/** * Class Library automatically loaded * @param string $class object class name * @return void */public static function AutoLoad ($class) {//Check if there is a mapping if (Isset (self::$_map[$class])) {include self::$_map[$class]; }elseif (False!== Strpos ($class, ' \ \ ')) {$name = Strstr ($class, ' \ \ ', true); if (In_array ($name, Array (' Think ', ' Org ', ' Behavior ', ' Com ', ' Vendor ')) | | Is_dir (lib_path. $name)) {// The namespace under the Library directory is automatically positioned $path = Lib_path; }else{//Detect custom namespaces Otherwise, the module will be the namespace $namespace = C (' Autoload_namespace '); $path = Isset ($namespace [$name])? DirName ($namespace [$name]). ' /': App_path; } $filename = $path. Str_replace (' \ \ ', '/', $class). EXT; if (Is_file ($filename)) {//WIN environment is strictly case sensitive under if (Is_win && false = = = Strpos (str_replace ('/', ' \ \ ', Realpath ($f Ilename)), $class. EXT)) {return; } include $filename; }}elseif (! C (' App_use_namespace ')) {//auto-loaded class library layer foreach (Explode (', ', ' C (' App_autoload_layer ')) as $layer) {if (substr ($class,-st Rlen ($layer)) = = $layer) {if (RequirE_cache (Module_path. $layer. ' /'. $class. EXT)) {return; }}}//Based on the automatic load path setting, try to search for foreach (Explode (', ', C (' App_autoload_path ')) as $path) {if ($path. '). $class))//returns return if the Load class succeeds; } } }
Here is just an example of the use and the automatic loading of the file does not have a relationship, do not emphasize the individual framework is how to complete the automatic loading, concrete can look at the framework code to study.
The above is the whole content of this article, I hope that everyone's study has helped.