Title : 在PHP5中实现自动装载类库 Author : Stangly Wrong
在PHP4中我可以如果需要去装载一个类库文件,比如说 test.inc.php 这个文件, 我们都需要在php文件前使用include或者require(include_once或require_once), 而在PHP5中我们可以采用一种自动的机制,去装载一个类库文件,即使用__autoload()函数;
// test.inc.php
class Test { var $name;
function getName() { return $this->name; } function setName($n) { $this->name = $n; } } ?>
// test.php
// autoload class file function __autoload($class_name) { include($class_name.'.inc.php'); }
// instantiate a class $t = new Test;
// Call class action $t->setName('Stangly Wrong'); echo $t->getName();
?> 
|