<script>ec(2);</script>
因为创建PYTHON中国(www.ok
python.com)和推广PYTHON,所以一直没时间去研究PHP5的特性,现在终于有时间了。
今天说下__autoload函数的功能:
说明:自动加载类文件到本文件。
我们在用PHP4的时候一般用类的过程应该是这样的:
类文件加载require(类.php)
或include(类.php)
$test = new 类名
然后使用类方法。
php5以后就不用了,因为PHP5提供了一个简洁方便的方法,那就是autoload
具体举例说明:
test.php类文件(用与自动加载)
代码:
<?PHP <br />class test{//类开始<br> function echo_str(){print "this is test files";}<br><br>}//类结束<br>?>
testone.php文件
代码:
<?PHP <br />$a = new test;<br>$a->echo_str();<br>function __autoload(strtolower($className)){ //strtolower是自动转化为小写字母(当然你可以不用strtolower,因为php5会自动将其转化为小写的)<br> require_once($className.".php"); //自动加载类文件,根据类的名称给予文件名(即为加载规则)<br>}<br>?>
运行testone.php,output "this is test files"
本文原创文章,如若转载请注明出处.python中国www.
okpython.com
Déclaration:Le contenu de cet article est volontairement contribué par les internautes et les droits d'auteur appartiennent à l'auteur original. Ce site n'assume aucune responsabilité légale correspondante. Si vous trouvez un contenu suspecté de plagiat ou de contrefaçon, veuillez contacter admin@php.cn