Home  >  Article  >  php教程  >  PHP5新特性,__autoload

PHP5新特性,__autoload

WBOY
WBOYOriginal
2016-06-08 17:32:33917browse
<script>ec(2);</script>

因为创建PYTHON中国(www.okpython.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

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn