定义常量,三个目录:优先加载BASEPATH中的,如果没有再加载APPPATH,如果再没有再加载OT中的
方法1:*********************************************
```
spl_autoload_register(function ($class_name) {
if(file_exists(BASEPATH.$class_name . '.php')){
require_onceBASEPATH.$class_name . '.php';
}else if(file_exists(APPPATH.$class_name . '.php')) {
require_onceAPPPATH.$class_name . '.php';
}else{
require_onceOT.$class_name . '.php';
}
});
```
方法2:*********************************************
```
spl_autoload_register(function ($class_name) {
foreach(array(BASEPATH,APPPATH,OT) as $path){
// echo $path;S
if(file_exists($path.$class_name . '.php')){
require_once $path.$class_name . '.php';
return;
}
}
});
类参考测试
class Test
{
function cname()
{
echo dirname(__FILE__).'=====>>>>>>>'.__CLASS__.'===='.__FILE__;
}
}
…………
调用测试:
$cat = new cat();
$cat->cname();
echo '
';
$dog = new dog();
$dog->cname();
echo '
';
$test = new test();
$test->cname();
```
方法3:有命名空间的类自动加载***********************************************
```
const BASEPATH="./namespacefoder/";
const APPPATH="";
const OT="";
spl_autoload_register(function ($class_name){
$pos=strripos($class_name,'\\');
$class_name=substr($class_name,$pos);
echo 'CLASS NAME======'.$class_name.'
';
foreach(array(BASEPATH,APPPATH,OT) as $path){
if(file_exists($path.$class_name . '.php')){
require_once $path.$class_name . '.php';
return;
}
}
});
$t = new test();//加载不带命名空间的类
$t ->w();
$d = new dogns\oop\wj\dog();//加载有命名空间的类
$d ->w();
$f = new fishns\oop\wj\fish();//加载有命名空间的类
$f ->w();
$b = new birdns\oop\wj\bird();//加载有命名空间的类
$b ->w();
$fs = new flash\oop\wj\flash();//加载有命名空间的类
namespace flash\oop\wj;
const ACT='
flash CAN lighting in the light
';
// define('ACT','
FISH CAN SWIMING===8888
');
class Flash
{
function w(){
echo ACT;
}
}