个人做游戏网站,广东网站开发设计,长治网站公司,哪个网站做简历免费## php自动加载 下面显示例子的文件目录结构图 一、没有使用命名空间的几种实现 test/oneClass.php class oneClass{public function show(){echo 这里是oneClass.php的show方法br/;}} test/twoClass.php ?phpclass twoClass{public function show(){…## php自动加载 下面显示例子的文件目录结构图 一、没有使用命名空间的几种实现 test/oneClass.php class oneClass{public function show(){echo 这里是oneClass.php的show方法br/;}} test/twoClass.php ?phpclass twoClass{public function show(){echo 这里是twoClass.php的show方法br/;}} 下面7种方式都可以实现自动加载结果都为: 这里是oneClass.php的show方法
这里是twoClass.php的show方法 方法一index.php 使用__autoload()魔术方法实现自动加载 ?php
//7.2以后使用这个提示一个警告,Deprecated: __autoload() is deprecated, use spl_autoload_register() instead
function __autoload($classname){include ./test/.$classname..php;
}//调用类库如果找不到会自动执行__autoload()
$one new oneClass();
$one-show();
$two new twoClass();
$two-show(); 运行结果 Deprecated: __autoload() is deprecated, use spl_autoload_register() instead in /Users/lidong/Desktop/wwwroot/test/April/autoload1/index.php on line 5
这里是oneClass.php的show方法
这里是twoClass.php的show方法 总结在PHP7.2以后使用__autoload()会报一个警告7.2之前这种方式是没提示的.这种方式是调用一个找不到的类会自动取调用__autoload()方法然后在方法里面执行include引用实现自动加载。 方法二index2.php 使用spl_autoload_register()方法实现自动加载创建自定义register方法调用 ?phpfunction register($classname){include ./test/{$classname}.php;
}spl_autoload_register(register);$one new oneClass();
$one-show();
$two new twoClass();
$two-show(); 方法三index3.php 使用spl_autoload_register()方法不定义register方法直接使用回调 ?phpspl_autoload_register(function($classname){include ./test/{$classname}.php;
});$one new oneClass();
$one-show();
$two new twoClass();
$two-show(); 方法四index4.php 使用spl_autoload_register()方法调用类的register方法实现自动加载 class autoLoad{public static function register($classname){include ./test/{$classname}.php;}
}spl_autoload_register([autoLoad,register]);$one new oneClass();
$one-show();
$two new twoClass();
$two-show(); 二、使用命名空间的几种实现 test2/oneClass.php ?phpnamespace auto\test2;
class oneClass{public function show(){echo 这里是oneClass.php的show方法br/;}} test2/twoClass.php ?php
namespace auto\test2;
class twoClass{public function show(){echo 这里是twoClass.php的show方法br/;}} 方法五index5.php使用spl_autoload_register()调用加载类的register方法转化传递过来的命名空间实现自动加载 ?phpclass autoLoad{public static function register($classname){$arr explode(\\, $classname);include ./test2/{$arr[2]}.php;}
}spl_autoload_register([autoLoad,register]);$one new auto\test2\oneClass();
$one-show();
$two new auto\test2\twoClass();
$two-show(); 方法六index6.php 跟方法五类似区别是use方法调用类实例化时可以直接使用类名实现自动加载 ?phpuse auto\test2\oneClass;
use auto\test2\twoClass;class autoLoad{public static function register($classname){$arr explode(\\, $classname);include ./test2/{$arr[2]}.php;}
}spl_autoload_register([autoLoad,register]);$one new oneClass();
$one-show();
$two new twoClass();
$two-show(); 方法七index7.php 与方法五和六思路一致只不过加载类放在外部不是引用在统一文件要点就是命名空间定义的类要使用也要先include,实现自动加载 autoLoad.php ?phpnamespace auto;
class autoLoad{public static function register($classname){$arr explode(\\, $classname);include ./test2/{$arr[2]}.php;}
} index7.php ?php
use auto\test2\oneClass;
use auto\test2\twoClass;include ./autoLoad.php;spl_autoload_register([auto\autoLoad,register]);$one new oneClass();
$one-show();
$two new twoClass();
$two-show(); 总结所有的自动加载思想都是调用一个没引用的类库会PHP会自动执行的一个加载方法这个方法有可能是类的方法也有可能是普通方法但不管怎么样都最终使用include执行文件包含只不过命名空间需要转化下获取类名。另外值得注意的是如果是一个php的框架自动加载实现也基本一致只不过他会根据不同文件夹下面的定义判断后include来实现不同文件夹下文件的引用来实现整个框架的自动加载。 转载于:https://www.cnblogs.com/lisqiong/p/10763793.html