我想在我創建的類中使用單例模式。我使用CodeIgniter,它似乎要求所有模型對象的構造函數公開。CodeIgniter - 不允許使用Singleton模式?
有沒有辦法解決這個問題?如果我不能使用Singleton模式,最好的辦法是什麼?
我的班級:
class RakebackSites extends CI_Model {
private $allSites = array();
private function __construct() {
parent::__construct();
self::initAllSites();
}
public static function getInstance() {
if(empty(self::$instance)) {
self::$instance = new CurrencyTypes();
}
return self::$instance;
}
private function initAllSites() {
$sql = "SELECT * FROM rakeback_sites";
$this->db->query($sql);
foreach ($q->result() as $row) {
$allSites[] = new RBSite($row->id, $row->name, $row->logo, $row->rakeback, $row->sign_up_bonus, $row->sign_up_bonus_currency, $row->referral_code, $row->id);
}
}
public function getAllSites() {
return $this->allSites;
}
}
和錯誤,我得到:
致命錯誤:訪問級別RakebackSites :: __結構()必須是公共的(如類CI_Model)在/ home /。 ../application/models/rakebacksites.php在線29
不幸的是,它告訴我它必須公開 – 2011-03-23 13:11:40