php类自动加载

__autoload

新建一个index.php

<?php

$d = new z();

function __autoload($class)  //自动捕获new的类名
{
	$file = $class . '.php';
	if(is_file($file)){
		include $file;
	}
}

新建一个z.php

<?php

class z
{
	function __construct(){
		echo 'z';
	}
}

spl_autoload_register

新建一个loader.php,也可以自动引入z类.

<?php   
class Loader   
{   
	public static function loadClass($class)   
	{   
		$file = $class . '.php';   
		if (is_file($file)) {   
			require_once($file);   
		}   
	}   
}
spl_autoload_register(array('Loader', 'loadClass'));   
$a = new z();

猜你喜欢

转载自www.cnblogs.com/fan-bk/p/10361527.html