php自动加载方式集合

2025-05-29 0 75

php加载文件方式:
1、include,include_once,requice,requice_one常规加载
2、__autoload()
3、spl_autoload_register()

常规加载方式
假设我们有一个类文件A.php,里面定义了一个名字为A的类:

?

1

2

3

4

5

6
<?php

class A {

public function __construct() {

echo 'Got it.';

}

}

然后我们有一个index.php需要用到这个类A,常规的写法就是

?

1

2

3
<?php

require('A.php');

$a = new A();

但是有一个问题就是,假如我们的index.php需要包含的不只是类A,而是需要很多类,这样子就必须写很多行require语句,有时候也会让人觉得不爽。

__autoload()自动加载
不过在php5之后的版本,我们就不再需要这样做了。
php5中,试图使用尚未定义的类时会自动调用__autoload函数,所以我们可以通过编写__autoload函数来让php自动加载类,而不必写一个长长的包含文件列表。

例如在上面那个例子中,index.php可以这样写:

?

1

2

3

4

5

6

7

8
<?php

function __autoload($class){

$file = $class . '.php';

if (is_file($file)) {

require_once($file);

}

}

$a = new A();

当然上面只是最简单的示范,__autoload只是去include_path寻找类文件并加载,我们可以根据自己的需要定义__autoload加载类的规则。注意:由于__autoload()是个函数,只能存在一次。

spl_autoload_register()自动加载
但现在问题来了,如果在一个系统的实现中,如果需要使用很多其它的类库,这些类库可能是由不同的开发人员编写的,其类名与实际的磁盘文件的映射规则不尽相同。这时如果要实现类库文件的自动加载,就必须在__autoload()函数中将所有的映射规则全部实现,这样的话__autoload()函数有可能 会非常复杂,甚至无法实现。最后可能会导致__autoload()函数十分臃肿,这时即便能够实现,也会给将来的维护和系统效率带来很大的负面影响。在这种情况下,难道就没有更简单清晰的解决办法了吧?答案当然是:NO!

spl_autoload_register() 满足了此类需求。 它实际上创建了 autoload 函数的队列,按定义时的顺序逐个执行。相比之下, __autoload() 只可以定义一次。

bool spl_autoload_register ([ callable $autoload_function [, bool $throw = true [, bool $prepend = false ]]] )
我们继续改写上面那个例子:

?

1

2

3

4

5

6

7

8

9
<?php

function loader($class){

$file = $class . '.php';

if (is_file($file)) {

require_once($file);

}

}

spl_autoload_register('loader');

$a = new A();

或者直接使用匿名函数:

?

1

2

3

4

5

6

7

8

9
<?php

spl_autoload_register(function($file){

$file = $class . '.php';

if (is_file($file)) {

require_once($file);

}

});

$a = new A();

这样子也是可以正常运行的,这时候php在寻找类的时候就没有调用__autoload而是调用我们自己定义的函数loader了。同样的道理,下面这种写法也是可以的:

?

1

2

3

4

5

6

7

8

9

10

11

12

13
<?php

class Loader {

public static function loadClass($class){

$file = $class . '.php';

if (is_file($file)) {

require_once($file);

}

}

}

spl_autoload_register(array('Loader', 'loadClass'));

//spl_autoload_register(array(__CLASS__, 'loadClass'));

//spl_autoload_register(array($this, 'loadClass'));

$a = new A();

更多示例
autoload.php

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21
<?php

define('SDK_PATH', __DIR__);

require_once SDK_PATH . '/common/functions.php';

require_once SDK_PATH . '/common/config.php';

spl_autoload_register(function ($class) {

if (false !== stripos($class, 'YJC\\Wechat')) {

require_once __DIR__ . '/' . str_replace('\\\\', DIRECTORY_SEPARATOR, substr($class, 10)) . '.php';

}

});

/*

function __autoload($class){

if (false !== stripos($class, 'YJC\\Wechat')) {

require_once __DIR__ . '/' . str_replace('\\\\', DIRECTORY_SEPARATOR, substr($class, 10)) . '.php';

}

}

*/

建议入口文件里养成定义绝对地址SDK_PATH的习惯,这样require不会出错。
以上就是本文的全部内容,希望对大家的学习有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 php自动加载方式集合 https://www.kuaiidc.com/98292.html

相关文章

发表评论
暂无评论