PHP5新特性,__autoload
今天说下__autoload函数的功能:
说明:自动加载类文件到本文件。
我们在用PHP4的时候一般用类的过程应该是这样的:
类文件加载require(类.php)
或include(类.php)
$test = new 类名
然后使用类方法。
php5以后就不用了,因为PHP5提供了一个简洁方便的方法,那就是autoload
具体举例说明:
test.php类文件(用与自动加载)
代码:
<?PHP
class test{//类开始 function echo_str(){print "this is test files";} }//类结束
?>
testone.php文件
代码:
<?PHP $a = new test; $a->echo_str(); function __autoload(strtolower($className)){ //strtolower是自动转化为小写字母(当然你可以不用strtolower,因为php5会自动将其转化为小写的) require_once($className.".php"); //自动加载类文件,根据类的名称给予文件名(即为加载规则) }
?>
运行testone.php,output "this is test files"
本文原创文章,如若转载请注明出处
文章网址:http://www.phprm.com/code/b6e63597244c6b51caf9040d9396dcb8.html
随意转载^^但请附上教程地址。