Re: Problems with Database Singleton Class
| From: | Martin Vidovic | Date: | Fri, 14 Apr 2006 17:57:56 +0000 |
| Subject: | Re: Problems with Database Singleton Class | ||
| References: | 1 | Groups: | php.windows |
| Request: | Send a blank email to php-windows+get-26842@lists.php.net to get a copy of this message | ||
On Friday 14 of April 2006 17:51, Vandegrift, Ken wrote:
> class WebDBConn {
>
> private static $instance = NULL;
>
> private function __construct() {
>
> self::$instance = new PDO($db, $user, $pwd);
> self::$instance->setAttribute(PDO::ATTR_ERRMODE,
> PDO::ERRMODE_EXCEPTION);
> }
>
> public static function getInstance() {
>
> if (!self::$instance) {
> self::$instance = new WebDBConn;
Here you overwrite your instance of PDO class with WebDBConn instance.
> }
> return self::$instance;
> }
> }
What you should do, is put the $instance initialization which you have in
__construct(), to the getInstance() method in place of 'self::$instance = new
WebDBConn'.
__construct() can be empty as you do not need an instance of WebDBConn
(everything is static).
Martin