Nasıl birbirleri ile iletişim çeşitli sınıfları içeren bir sınıf oluşturmak mı?

0 Cevap php

I'm trying to take full advantage of object oriented php and learn something along the way. Following an MVC tutorial I was able to do this:

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);

Sonra her sınıf bu şablonu takip edecek:

class Database{

    private $repo;

    function __construct($repo) {
        $this->repo= $repo;
    }
}

Ben ana sınıfı bunu önce ben örnekte varım bir önce yüklenen tüm sınıfların yöntemlerini ve değişkenler erişebilirsiniz Bu şekilde:

$this->repo->db->someMethod();

Beni çarptığı şey nesnesi $ repo bu akıllıca bir sorun bellek yeni bir sınıf yüklenen her zaman çoğaltılır olur olduğunu? Bunu yapmak için daha iyi yollar var mı? Bu gerçek bir projede kullanılabilir bir şey mi?

Thank you very much

0 Cevap