Ben şu anda bir proje için mini çerçeve çeşit inşa, ve bu çözüm ile gelip ediyorum. Ben çoğu denedi, ama bu (kod basitlik için kısaltılır) benim için çok rahat görünüyor:
# Basically it's just a Registry pattern
class Repository {
private static $objects = array();
public function loadObject($alias, $object) {
self :: $objects[$alias] = $object;
return true;
}
public function __get($name) {
if ($this->objectExists($name)) {
return self::$objects[$name];
} else {
return false;
}
}
}
class Database extends Repository {
/* database class */
}
class Session extends Repository {
public function some_func($key, $value) {
/* i can access database object using $this in any class that extends Repository */
$this -> database -> exec (/* sql */);
}
}
/* =================== */
# Load core objects
$R = new Repository :: getInstance();
$R -> loadObject ('config', new Config());
$R -> loadObject ('database', new Database());
$R -> loadObject ('session', new Session());
/* =================== */
Can you see any problems or drawbacks with this approach? For me i see maybe i little more memory consumption, because each next class holds more and more objects from Repository. Before i had a design where each class was independent, but anyway all of them require database, session, config etc, no i had to declare them in any class. Just wanted to note that i'm planning this design only for core objects, not for specific classes.