How PHP framework automatically loads files-php Tutorial

Source: Internet
Author: User
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!

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.