- /* autoload.php */
- function __autoload($classname) {
- require_once ($classname . “class.php”);
- }
- $person = new Person(”Altair”, 6);
- var_dump ($person);
- ?>
- $paths[] = BP . DS . ‘app’ . DS . ‘local’;
- $paths[] = BP . DS . ‘app’ . DS . ‘base’;
- $paths[] = BP . DS . ‘lib’;
- $appPath = implode(PS, $paths);
- set_include_path($appPath . PS . get_include_path());
- class Autoload {
- /**
- * 自身对象
- *
- */
- protected static $_instance = null;
- public function __construct() {
- }
- /*
- * 实例化自身
- *
- */
- public static function instance() {
- if (null == self::$_instance) {
- self::$_instance = new self();
- }
- return self::$_instance;
- }
- /**
- *
- * 注册自动加载函数
- */
- public static function register() {
- spl_autoload_register(array(self::instance(), ‘autoload’));
- }
- /*
- *
- * 自动加载类
- */
- public function autoload($class) {
- if (!is_string($class)) {
- return;
- }
- $classFile = str_replace(‘ ‘, DS, ucwords(str_replace(‘_’, ‘ ‘, $class)));
- $classFile .= ‘.php’;
- return include $classFile;
- }
- }