cakephp 中spl_autoload_register用法

使用spl_autoload_register() 自定义的方法来加载文件语法:bool spl_autoload_register ( [callback $autoload_function] ) 

 

1.在cakephp中使用(bootstrap.php):

1).通过数组的形式传递类和方法,元素一为类名称、元素二为方法名称

2).方法为静态方法

 

spl_autoload_register(array('App', 'load'));

 

2.App下的load方法(core/App.php)

 

/**
 * 处理自动加载类的方法.它会找到每个类包
 * 使用App::uses()来定义并且带有解决全路径包名的信息
 * 每个类



的



文件



名称



应遵循



的类的名称








 * 例如:类名`MyCustomClass` 文件 `MyCustomClass.php`

 * @param string $className the name of the class to load
 * @return boolean
 */
	public static function load($className) {
               //protected static $_classMap = array(); 通过App::uses()加入的: $className=>$location
                if (!isset(self::$_classMap[$className])) {
			return false;
		}
		if ($file = self::_mapped($className)) {
			return include $file;
		}
                //用点号分开plugin文件夹
		$parts = explode('.', self::$_classMap[$className], 2);
		list($plugin, $package) = count($parts) > 1 ? $parts : array(null, current($parts));
		$paths = self::path($package, $plugin);

		if (empty($plugin)) {
			$appLibs = empty(self::$_packages['Lib']) ? APPLIBS : current(self::$_packages['Lib']);
			//优先app lib
                        $paths[] =  $appLibs . $package . DS;
			$paths[] = CAKE . $package . DS;
		}

		foreach ($paths as $path) {
			$file = $path . $className . '.php';
			if (file_exists($file)) {
				self::_map($file, $className);
                                return include $file; //include file and can new class
			}
		}

		//To help apps migrate to 2.0 old style file names are allowed
		foreach ($paths as $path) {
			$underscored = Inflector::underscore($className);
			$tries = array($path . $underscored . '.php');
			$parts = explode('_', $underscored);
			if (count($parts) > 1) {
				array_pop($parts);
				$tries[] = $path . implode('_', $parts) . '.php';
			}
			foreach ($tries as $file) {
				if (file_exists($file)) {
					self::_map($file, $className);
					return include $file;
				}
			}
		}

		return false;
	}

 

3.

App::uses('Dispatcher', 'Routing');

$Dispatcher = new Dispatcher();
	
$Dispatcher->dispatch(new CakeRequest(), new CakeResponse(array('charset' => Configure::read('App.encoding'))));
 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值