I want to put my Zend Model as Singleton, so I have done this:
class Social_Model_DbTable_Dossier extends Zend_Db_Table_Abstract {
private static $_instance;
public static function GetInstance() {
if (!self::$_instance instanceof self) {
self::$_instance = new self();
}
return self::$_instance;
}
private function __construct() {
// put normal constructor code.
// it will only ever be called once
}}
I instantiate my model like so:
$dossiercasModel = Social_Model_DbTable_Dossier::GetInstance();
but this erreur is occured:
Fatal error: Access level to Social_Model_DbTable_Dossier::__construct() must be public (as in class Zend_Db_Table_Abstract)
when I put the constructor of the Model as public it works fine but this is inconsistent with the notion of singleton!
In the past I have gotten arround having to do this by creating a table broker that would serve up cached table instances, kind of a multiton.
a simple example
class My_TableManager{
protected static $_instance;
protected $_tableCache = array();
protected function __construct(){
}
public static function getInstance(){
if (!isset(self::$_instance)) self::$_instance = new self();
}
public function getTable($tableName){
if (!array_key_exists($tableName, $this->_tableCache)){
// you can do fun stuff here like name inflection
// Im assuming that tables will be suffixed with _Table
$tableClass = "My_".$tableName."_Table";
$this->_tableCache[$tableName] = new $tableClass();
}
return $this->_tableCache[$tableName];
}
public static function get($tableName){
return self::getInstance()->getTable($tableName);
}
}
To use to get an instance of My_User_Table you would could:
$table = My_TableManager::get("My_User");
or
$table = My_TableManager::getInstnace()->getTable("My_Table");