Use s 0 en to automatically load the implementation code of the undefined class using s 1 en of

  • 2020-05-30 19:41:57
  • OfStack

Following is a section of code using s 1en () for your reference:


<?php
/**
*  Automatically loads 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 paths:


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


Note: convert the underscore to a directory delimiter (DIRECTORY_SEPARATOR), which effectively manages library files and solves cross-platform problems.


Related articles: