The implementation code for using __autoload () in PHP to automatically load undefined classes _php tips

Source: Internet
Author: User

Here is a section using __autoload () code, for everyone to learn the reference:

Copy Code code as follows:

<?php
/**
* Automatically load related class 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 way to include a path:

Copy Code code as follows:

<?php
function __autoload ($class _name) {
$path = Str_replace (' _ ', Directory_separator, $class _name);
require_once $path. ' PHP ';
}
?>


Description: Converts underscores to directory delimiters (Directory_separator), which enables you to 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.