spl_autoload_register自动加载介绍
2015-03-06 14:22
543 查看
最经常使用的就是这种方法,根据类名,找出类文件,然后require_once
function __autoload($class_name) {
$path = str_replace('_', '/', $class_name);
require_once $path . '.php';
}
// 这里会自动加载Http/File/Interface.php 文件
$a = new Http_File_Interface();
这种方法的好处就是简单易使用。当然也有缺点,缺点就是将类名和文件路径强制做了约定,当修改文件结构的时候,就势必要修改类名。
__autoload的最大缺陷是无法有多个autoload方法
好了, 想下下面的这个情景,你的项目引用了别人的一个项目,你的项目中有一个__autoload,别人的项目也有一个__autoload,这样两个__autoload就冲突了。解决的办法就是修改__autoload成为一个,这无疑是非常繁琐的。
因此我们急需使用一个autoload调用堆栈,这样spl的autoload系列函数就出现了。你可以使用spl_autoload_register注册多个自定义的autoload函数
例如:
A.php
<?php
class A
{
public function __construct()
{
echo 'Got it A.';
}
}
B.php
<?php
class B
{
public function __construct()
{
echo 'Got it B.';
}
}
?>
test.php
<?php
function __autoload($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
$a = new A();
$b = new B();
运行可以得到:Got it A.Got it B.
还有一种写法:
<?php
class Loader
{
public static function loadClass($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
}
spl_autoload_register(array('Loader', 'loadClass'));
$a = new A();
function __autoload($class_name) {
$path = str_replace('_', '/', $class_name);
require_once $path . '.php';
}
// 这里会自动加载Http/File/Interface.php 文件
$a = new Http_File_Interface();
这种方法的好处就是简单易使用。当然也有缺点,缺点就是将类名和文件路径强制做了约定,当修改文件结构的时候,就势必要修改类名。
__autoload的最大缺陷是无法有多个autoload方法
好了, 想下下面的这个情景,你的项目引用了别人的一个项目,你的项目中有一个__autoload,别人的项目也有一个__autoload,这样两个__autoload就冲突了。解决的办法就是修改__autoload成为一个,这无疑是非常繁琐的。
因此我们急需使用一个autoload调用堆栈,这样spl的autoload系列函数就出现了。你可以使用spl_autoload_register注册多个自定义的autoload函数
例如:
A.php
<?php
class A
{
public function __construct()
{
echo 'Got it A.';
}
}
B.php
<?php
class B
{
public function __construct()
{
echo 'Got it B.';
}
}
?>
test.php
<?php
function __autoload($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
$a = new A();
$b = new B();
运行可以得到:Got it A.Got it B.
还有一种写法:
<?php
class Loader
{
public static function loadClass($class)
{
$file = $class . '.php';
if (is_file($file)) {
require_once($file);
}
}
}
spl_autoload_register(array('Loader', 'loadClass'));
$a = new A();
相关文章推荐
- PHP 命名空间与自动加载机制介绍,spl_autoload_register, __autoload 函数
- PHP自动加载类—__autoload()和spl_autoload_register() (补充)
- php spl_autoload_register类库自动加载,省去引入类文件
- php面向对象(OOP)—spl_autoload_register()注册自定义的自动加载函数
- spl_autoload_register 注册自己的自动加载函数(__autoload())
- PHP自动加载类—__autoload()和spl_autoload_register()
- PHP自动加载机制[2]---spl_autoload_register操作详解
- PHP类的自动加载__autoload && spl_autoload_register
- 使用spl_autoload_register实现自动加载
- php自动加载类的实现 spl_autoload_register
- 12-31--PHP函数spl_autoload_register实现自动加载研究
- 12-31--PHP函数spl_autoload_register实现自动加载研究
- 详解spl_autoload_register()函数以及自动加载不同目录的类
- __autoload,spl_autoload_register与自动加载
- PHP自动加载上——spl_autoload_register
- PHP spl_autoload_register实现自动加载研究
- spl_autoload_register 让类在需要的时候自动加载
- PHP自动加载 spl_autoload_register
- PHP spl_autoload_register实现自动加载研究
- PHP自动加载类—__autoload()和spl_autoload_register() (补充)