Use _ autoload () in php to automatically load the implementation code of undefined classes

Source: Internet
Author: User
This method is triggered when the PHP engine encounters a class that is not instantiated. of course, you need to use _ autoload () in your php code. Below is a section using _ autoload () for your reference:

The code is as follows:
/**
* Automatically load related library files
*/
Function _ autoload ($ classname ){
If (substr ($ classname,-6) = "Action "){
Include APP_PATH. 'controllers/'. $ classname.'. class. php ';
} Elseif (substr ($ classname,-5) = "Model "){
Include APP_PATH. 'Models/'. $ classname.'. class. php ';
} Elseif ($ classname = "Smarty "){
Include SYSTEM_PATH. 'smarty/smarty. class. php ';
} Else {
Include APP_PATH. 'common/'. $ classname.'. class. php ';
}
}
?>

Another method for path inclusion:

The code is as follows:
Function _ autoload ($ class_name ){
$ Path = str_replace ('_', DIRECTORY_SEPARATOR, $ class_name );
Require_once $ path. '. php ';
}
?>


Note: the underline is converted to the directory separator (DIRECTORY_SEPARATOR). This can effectively manage library files and solve cross-platform problems.

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.