Home  >  Article  >  Backend Development  >  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

WBOY
WBOYOriginal
2016-07-21 15:13:53678browse

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...
Statement:
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