我正试图充分利用面向对象的php,并在此过程中学习一些东西。
class Repository {
private $vars = array();
public function __set($index, $value){
$this->vars[$index] = $value;
}
public function __get($index){
return $this->vars[$index];
}
}
/*
*/
function __autoload($class_name) {
$filename = strtolower($class_name) . '.class.php';
$path = dirname(__FILE__);
$file = $path.'/classes/' . $filename;
if (file_exists($file) == false){
return false;
}
include ($file);
}
//Main class intialization
$repo = new Repository;
//Configuration loading
$repo->config = $config;
//Classes loading
$repo->common = new Common($repo);
$repo->db = new Database($repo);
$repo->main = new Main($repo);
然后每个类将遵循以下模板:
class Database{
private $repo;
function __construct($repo) {
$this->repo= $repo;
}
}
这样我就可以访问在我所在的类之前加载的类的所有方法和变量。在主类中执行此操作之前的示例中:
$this->repo->db->someMethod();