This article describes in detail the principles of PHP framework automatic file loading, which has some reference value, interested friends can refer to this article for details about the principles of PHP framework automatic file loading, which has some reference value. interested friends can refer
Description:The company's project PHP is used as the intermediate forwarding layer (receiving http requests, using socket to communicate with c ++). because the code is not used in the framework, these things are naturally written by the previous people themselves. We need to optimize the underlying layer recently, so I read this part of code.
Purpose:The main function of this code is to load all the plug-in classes in the main directory at one time. This function is automatically loaded when you use a class or interface that has not been defined. By registering the automatic loader, the script engine has the last chance to load the required class before a PHP error fails.
Implementation method:Mainly used in PHP function _ autoload ()
Details:
Error_reporting (E_ERROR | E_WARNING | E_PARSE | E_NOTICE); set_include_path ($ _ SERVER ['root _ path']. '/Libs '. PATH_SEPARATOR. $ _ SERVER ['root _ path']. '/lib '. PATH_SEPARATOR. get_include_path (); if (! Function_exists ('_ autoload') {function _ autoload ($ className) {// optimize the inclusion path $ path = _ getRootPath ($ className ); $ revpath = strtr ($ className ,'_','/'). '. php '; $ rootpath = $ path. $ revpath; file_exists ($ rootpath )? Include ($ rootpath): @ include ($ revpath) ;}}/*** get the root path **/function _ getRootPath ($ classname) {$ pearpath = $ _ SERVER ["PHP_PEAR_PATH"]. '/'; $ libpath = $ _ SERVER ['root _ path']. '/lib/'; $ libspath = $ _ SERVER ['root _ path']. '/libs/'; if (strpos ($ classname, 'zend _ ') === 0) return $ pearpath; // Zend Framework path if (strpos ($ classname, 'db _ ') = 0 | strpos ($ classname, 'interface _') = 0 | strpos ($ classname, 'Oss _') = 0 | strpos ($ classname, 'pay _ ') = 0 | strpos ($ classname, 'phpmailer _') = 0) return $ libspath; return $ libpath ;}
The _ getRootPath ($ classname) function obtains the real directory where the class name file is located, and determines the directory under which the class name is located based on the class name header field;
If the class can be found in these directories, the class will be loaded before use.
The above is a detailed description of the principles of PHP framework automatic loading files. For more information, see other related articles in the first PHP community!