自动加载函数__autoload和spl_autoload_register

<?php

//自动加载函数
//__autoload函数只能全局的声明一次,整个php项目的运行中,它只能声明一次
function __autoload($className)
{
		$path='./'.$className.'.php';

		if(!file_exists($path)){
				throw new Exception('file not found.');
		}

		require_once $path;
}


//spl_autoload_register:多次注册,队列执行


$a=new A();
$a->eat();
			
?>
<?php


//spl_autoload_register:多次注册,队列执行

spl_autoload_register(function($className)
{
	$filePath='./'.$className.'.php';

	if(!file_exists($filePath)){
			throw new Exception('file not found.');
	}

	require_once $filePath;
});


$a=new A();
$a->eat();
			
?>
<?php

class A{
	public function eat()
	{
			echo '吃';		
	}
}


?>

  

posted @ 2018-02-23 10:06  唔愛吃蘋果  阅读(682)  评论(0)    收藏  举报