Home > Backend Development > PHP Tutorial > Use __autoload() to automatically load the implementation code of undefined classes in PHP_PHP Tutorial

Use __autoload() to automatically load the implementation code of undefined classes in PHP_PHP Tutorial

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-07-21 15:13:53
Original
764 people have browsed it

The following is a piece of code using __autoload() for your reference:

Copy code The code is as follows:

/**
* 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 the path:

Copy the code The code is as follows:

function __autoload($class_name) {
$path = str_replace('_', DIRECTORY_SEPARATOR, $class_name);
require_once $path.'.php';
}
?>


Description: Convert the underscore to the directory separator (DIRECTORY_SEPARATOR). This can effectively manage library files and solve cross-platform problems.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/326444.htmlTechArticleThe following is a piece of code using __autoload() for your reference: Copy the code as follows: ?php / *** Automatically load related class library files*/ function __autoload($classname){ if(su...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template