PHP Singleton Class - Fatal error: Call to private MyObject::__construct()
Singleton Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | <?php class db_singleton { const ORACLE_HOST ="SOMEIP"; const ORACLE_USER ="validuser"; const ORACLE_PASS ="validpass"; const ORACLE_DB ="SOMEIP/DBNAME"; private static $instance; // stores the oci_* instance private function __construct() { } // block directly instantiating private function __clone() { } // block cloning of the object public static function call() { // create the instance if it does not exist if(!isset(self::$instance)) { // the ORACLE_* constants should be set to or // replaced with your db connection details self::$instance = oci_connect(self::ORACLE_USER, self::ORACLE_PASS, self::ORACLE_DB); if(self::$instance->connect_error) { throw new Exception('Oracle connection failed: ' . self::$instance->connect_error); } } // return the instance return self::$instance; } public function __destruct() { oci_close($instance); } public function queryresult($query) { $result_set_array =array(); $this->stmt = oci_parse($this->con, $query); oci_execute($this->stmt); while($row=oci_fetch_array($this->stmt,OCI_ASSOC+OCI_RETURN_NULLS)) { $result_set_array[] = $row; } oci_free_statement($this->stmt); return $result_set_array; } } ?> |
当我尝试使用
ZZU1
现在,当我尝试用模型扩展我的班级时,它飞过了例外。
1 2 3 4 5 6 7 8 9 | class Myclass Extends db_singleton{ public function someModel() { $result = parent::queryresult(" select * from somevalid_table"); return $result; } } |
例外:
1 | Fatal error: Call to private db_singleton::__construct() from context 'someController' |
我知道班级不能安装有私人建筑师。=UU ^ U Construction()Functions are always called when an object is instantiated,so trying to do something like$X=new myobject()will cause a datal error with a private construction function.
我正在使用
谢谢
PHP类脚本:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | class db_singleton { const ORACLE_USER ="validuser"; const ORACLE_PASS ="validpass"; const ORACLE_DB ="SOMEIP/DBNAME"; private static $instance = null; // stores the oci_* instance // private constructor private function __construct() { } // block directly instantiating private function __clone() { trigger_error('Clone is not allowed.', E_USER_ERROR); } // block cloning of the object public static function getInstance() { // create the instance if it does not exist if(!isset(self::$instance)) { // the ORACLE_* constants should be set to or // replaced with your db connection details self::$instance = oci_connect(self::ORACLE_USER, self::ORACLE_PASS, self::ORACLE_DB); if(self::$instance->connect_error) { throw new Exception('Oracle connection failed: ' . self::$instance->connect_error); } } // return the instance return self::$instance; } public static function queryresult($query) { $result_set_array =array(); $stmt = oci_parse(db_singleton::getInstance(), $query); oci_execute($stmt); while($row=oci_fetch_array($stmt,OCI_ASSOC+OCI_RETURN_NULLS)) { $result_set_array[] = $row; } oci_free_statement($stmt); return $result_set_array; } |
现在为了防止
1 2 3 4 5 6 7 8 9 10 | class Myclass Extends db_singleton{ public function __construct() {} public function someModel(){ $result = parent::queryresult(" select * from somevalid_table"); return $result; } } |
希望它能帮助别人。
谢谢。
如果您的构造函数在该类中是私有的,那么
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | Create a public method /** * Singleton class * */ final class UserFactory { /** * Call this method to get singleton * * @return UserFactory */ public static function Instance() { static $inst = null; if ($inst === null) { $inst = new UserFactory(); } return $inst; } /** * Private ctor so nobody else can instance it * */ private function __construct() { } } |
使用:
1 2 3 4 | $fact = UserFactory::Instance(); $fact2 = UserFactory::Instance(); $fact == $fact2; |
但是:
1 | $fact = new UserFactory() |