我正在研究將支持多種數據庫方法的MPTT對象。首先是MySQL和MySQLi。現在,我已經創造了它這樣返回對象實例的引用
MPTT - 主要對象,將加載正確的子對象
class Mptt {
/**
* Array of available driver types
* @var array
*/
private $availableDrivers = array('mysqli','mysql');
/**
* Holding an instance of the mptt object corresponding to the selected driver
* @var object
*/
public $instance;
public function __construct($driver = 'mysqli', $autoConnect = false, $info = array()) {
if (in_array($driver, $this->availableDrivers)) {
switch ($driver) {
case 'mysqli':
$this->instance =& new Mptt_MySQLi();
break;
case 'mysql':
$this->instance =& new Mptt_MySQL();
break;
}
return $this->instance;
}
}
}
現在,我已經全成在得到這個工作的唯一方法是像做
添加公共變量爲每個驅動程序,並像這樣做
$mptt = new Mptt('mysqli');
$mptt->mysqli->addBranch(.....);
,但我不希望mysqli-> part
..所以我想如果我也許試圖將$this->instance
通過作爲REFFERENCE然後$mptt
將reffer到Mptt_MySQLi
代替..
希望有人知道一個答案..
預先感謝 - 奧萊
看看這個:http://components.symfony-project.org/dependency-injection/ – Mchl