Jump to content

MySQLi Extended w/Singleton Gives Problems


julesmanson

Recommended Posts

Extending mysqli and adding new functions was not a problem but as soon as I applied embedded singleton pattern my extended mysqli class would not recognize any functions not originally named in the mysqli class. Below I show the skeleton code for both versions. What I need is some work around so that I can have both - singleton and my own named functions. Thank you so much for helping.

 

$db2 = new Connect2();

$db2->testy();

// Connect2 has no singleton applied and gives good results:

// Connected to database!

// Connect2 class recognizes testy()!

 

$db1 = Connect1::getInstance();

$db1->testy();

// Connect1 has singleton applied and gives bad results:

// Fatal error: Call to undefined method mysqli::testy() in... blah blah blah

 

class Connect2 extends mysqli{# no singleton applied, thia one works fine

private $host,$user,$pass,$dbas;

public function __construct($host=HOST,$user=USER,$pass=PASS,$dbas=JMAN){

parent::__construct($host,$user,$pass,$dbas);

$this->host = $host;

$this->user = $user;

$this->pass = $pass;

$this->dbas = $dbas;

$this->check();

}

public function testy(){echo '<p>Connect2 class recognizes <i><b>testy()</i></b>!</p>';}

private function check(){# check if connected

if(mysqli_connect_error()) echo '<p>Could not connect to database!</p>';

else echo '<p>Connected to database!</p>';

}

}

 

class Connect1 extends mysqli{# with embedded singleton, works fine except for mew functiona not originally named in mysqli

private $host,$user,$pass,$dbas;

private static $inst;

private function __construct($host=HOST,$user=USER,$pass=PASS,$dbas=JMAN){

parent::__construct($host,$user,$pass,$dbas);

$this->host = $host;

$this->user = $user;

$this->pass = $pass;

$this->dbas = $dbas;

$this->check();

}

public function testy(){echo '<p>Connect1 class recognizes <i><b>testy()</i></b>!</p>';}

public static function getInstance($host=HOST,$user=USER,$pass=PASS,$dbas=JMAN){# embedded singleton

if(!self::$inst) self::$inst = new Mysqli($host,$user,$pass,$dbas);

return self::$inst;

}

private function check(){# check if connected

if(mysqli_connect_error()) echo '<p>Could not connect to database!</p>';

else echo '<p>Connected to database!</p>';

}

}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.