Re: green bean question on singleton php5

From: Date: Wed, 16 Jan 2008 18:54:01 +0000
Subject: Re: green bean question on singleton php5
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-267430@lists.php.net to get a copy of this message
julian schreef:
Hi, I am implementing this
try comparing this rewrite with your version: abstract class dbaccess { static $db = null; private static function init() {
    if (dbaccess::$db))
return;
    dbaccess::$db = new mysqli("localhost",USER,PASSWD,DB);
    if(mysqli_connect_errno()) 	
throw new Exception('cannot connect to DB'); } public static function getDB() { self::init(); return dbaccess::$db; } } try { $db = dbaccess::getDB(); $db->query("SELECT foo FROM bar WHERE qux=1"); } catch (Exception) { die($e->getMessage()); }
class dbaccess{ static $db=null; static $othervar=33;
      private  function dbaccess(){
    dbaccess::$db= new mysqli("localhost",USER,PASSWD,DB);
    if(mysqli_connect_errno()){
      echo "no way";
    }
}
    public static function GetDb(){
if(dbaccess::$db==null){
dbaccess is a static class, you should not be constructing it here it!
dbaccess::$db= new dbaccess(); } return dbaccess::$db;
      }
} $db=dbaccess::GetDE(); $db->query(..................); will fail...with Call to undefined method dbaccess::query() apparently $db is of type dbaccess... and thus has not does not have query implemented.... any hhelp appreciated..... JCG


« previous php.general (#267430) next »