当前位置: 编程技术>php
php自动加载autoload机制示例分享
来源: 互联网 发布时间:2014-08-26
本文导语: 1,自定义函数 2,spl_autoload_register() 代码如下:liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ ll ./*-rw-rw-r-- 1 liuyuan liuyuan 800 Feb 19 11:39 ./func_autoload.php-rw-rw-r-- 1 liuyuan liuyuan 906 Feb 19 11:28 ./spl_autoload.php./include:total 16drwxrwxr-x 2 liuyuan liuyuan 4096...
1,自定义函数
2,spl_autoload_register()
代码如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ ll ./*
-rw-rw-r-- 1 liuyuan liuyuan 800 Feb 19 11:39 ./func_autoload.php
-rw-rw-r-- 1 liuyuan liuyuan 906 Feb 19 11:28 ./spl_autoload.php
./include:
total 16
drwxrwxr-x 2 liuyuan liuyuan 4096 Feb 19 11:42 ./
drwxrwxr-x 3 liuyuan liuyuan 4096 Feb 19 11:43 ../
-rw-rw-r-- 1 liuyuan liuyuan 142 Feb 19 11:42 aClass.php
-rw-rw-r-- 1 liuyuan liuyuan 143 Feb 19 11:42 bClass.php
首先看自定义函数方式:
代码如下:
运行结果如下:
代码如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ php func_autoload.php
Array
(
[0] => /var/www/phpgcs/php_autoload/func_autoload.php
)
.:/usr/share/php:/usr/share/pear
aClass is loaded
bClass is loaded
Array
(
[0] => /var/www/phpgcs/php_autoload/func_autoload.php
[1] => /var/www/phpgcs/php_autoload/include/aClass.php
[2] => /var/www/phpgcs/php_autoload/include/bClass.php
)
第二种方式:
代码如下:
运行结果如下:
代码如下:
liuyuan@ebuinfo:/var/www/phpgcs/php_autoload$ php spl_autoload.php
aClass is loaded
Array
(
[0] => /var/www/phpgcs/php_autoload/spl_autoload.php
[1] => /var/www/phpgcs/php_autoload/include/aClass.php
)
bClass is loaded