- 自动加载方法:
function my_autoload($name){ $name = str_replace('\\',DIRECTORY_SEPARATOR,$name); require $name.'.class.php'; }
- 注册:
spl_autoload_register(__NAMESPACE__.'\\my_autoload');
- 代码:
a. 入口文件:index.php<?php namespace Index; use auto\say; use auto\mycolor\color; function my_autoload($name){ $name = str_replace('\\',DIRECTORY_SEPARATOR,$name); require $name.'.class.php'; } spl_autoload_register(__NAMESPACE__.'\\my_autoload'); $say = new say(); $say->hello(); $color = new color(); $color->test();
b. auto/say.class.php:
<?php namespace auto; class say{ public function hello(){ echo "hello"; } }
c. auto/mycolor/color.class.php:
<?php namespace auto\mycolor; class color{ public function test(){ echo "blue"; } } 本文转自 bilinyee博客,原文链接: http://blog.51cto.com/12173069/2071302 如需转载请自行联系原作者