倒霉的大熊

座右铭:天时地利人和

导航

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();

 

posted on 2019-02-11 13:15  倒霉的大熊  阅读(98)  评论(0编辑  收藏  举报