Accessing a PDO DB object from within Singleton pattern

Accessing a PDO DB object from within Singleton pattern

am 01.10.2007 19:49:37 von Macca

Hi, quite new to design patterns so please humour me.

I'm having a little trouble figuring out how to use the singleton
pattern to create a database PDO object that I can then refer to in my
script.

I created the instance with this:


class PDO_Singleton{
private static $_singleton;
private $_connection;


private function __construct()
{
$this->_connection = new PDO('mysql:host=localhost;dbname=pm5494',
'root', 'password');
}

public static function getInstance()
{
if (is_null (self::$_singleton)){
self::$_singleton = new PDO_Singleton;
}
return self::$_singleton;
}
}


$dbh = PDO_Singleton::getInstance();



OK, So now I have my instance and i want to use my PDO object, if try
this:

$statemant = $dbh->_connection->prepare("select * from pans");

Which obviously gives me the message "Cannot access private property
PDO_Singleton::$_connection" because $_connection is declared private.


So how am I supposed to use my PDO object without having to declare it
public or access it from within my PDO_Singleton class?


Help appreciated,

Regards,

Paul

Re: Accessing a PDO DB object from within Singleton pattern

am 01.10.2007 20:42:43 von zeldorblat

On Oct 1, 1:49 pm, macca wrote:
> Hi, quite new to design patterns so please humour me.
>
> I'm having a little trouble figuring out how to use the singleton
> pattern to create a database PDO object that I can then refer to in my
> script.
>
> I created the instance with this:
>
> class PDO_Singleton{
> private static $_singleton;
> private $_connection;
>
> private function __construct()
> {
> $this->_connection = new PDO('mysql:host=localhost;dbname=pm5494',
> 'root', 'password');
> }
>
> public static function getInstance()
> {
> if (is_null (self::$_singleton)){
> self::$_singleton = new PDO_Singleton;
> }
> return self::$_singleton;
> }
>
> }
>
> $dbh = PDO_Singleton::getInstance();
>
> OK, So now I have my instance and i want to use my PDO object, if try
> this:
>
> $statemant = $dbh->_connection->prepare("select * from pans");
>
> Which obviously gives me the message "Cannot access private property
> PDO_Singleton::$_connection" because $_connection is declared private.
>
> So how am I supposed to use my PDO object without having to declare it
> public or access it from within my PDO_Singleton class?
>
> Help appreciated,
>
> Regards,
>
> Paul

Add a public method to the class that looks like this:

public function prepare($stmt) {
return $this->_connection->prepare($stmt);
}

Then call it like this:

$dbh = PDO_Singleton::getInstance();
$statemant = $dbh->prepare("select * from pans");

If you want to make your instance variables private you'll need some
public method to access and manipulate them from the outside.

Re: Accessing a PDO DB object from within Singleton pattern

am 02.10.2007 03:49:57 von Michael Fesser

..oO(macca)

>Hi, quite new to design patterns so please humour me.
>
>I'm having a little trouble figuring out how to use the singleton
>pattern to create a database PDO object that I can then refer to in my
>script.
>
>[...]
>
>$dbh = PDO_Singleton::getInstance();
>
>
>
>OK, So now I have my instance and i want to use my PDO object, if try
>this:
>
>$statemant = $dbh->_connection->prepare("select * from pans");
>
>Which obviously gives me the message "Cannot access private property
>PDO_Singleton::$_connection" because $_connection is declared private.
>
>
>So how am I supposed to use my PDO object without having to declare it
>public or access it from within my PDO_Singleton class?

Two ways come to mind:

1) Make your singleton class extend the PDO class. This way in the code
above $dbh itself would become the PDO instance, so you could call

$dbh->prepare(...);

2) Use the decorator pattern. The decorator (your singleton) has to
forward all method calls to the decorated object ($_connection), either
by explicitly writing a method for each or by using the magic __call()
interceptor method. I use the latter in my own database class:

protected function __call($method, $arguments) {
return call_user_func_array(
array($this->driver, $method),
$arguments
);
}

(The $driver property would be $_connection in your case.)

Micha

Re: Accessing a PDO DB object from within Singleton pattern

am 02.10.2007 05:07:32 von Jerry Stuckle

macca wrote:
> Hi, quite new to design patterns so please humour me.
>
> I'm having a little trouble figuring out how to use the singleton
> pattern to create a database PDO object that I can then refer to in my
> script.
>
> I created the instance with this:
>
>
> class PDO_Singleton{
> private static $_singleton;
> private $_connection;
>
>
> private function __construct()
> {
> $this->_connection = new PDO('mysql:host=localhost;dbname=pm5494',
> 'root', 'password');
> }
>
> public static function getInstance()
> {
> if (is_null (self::$_singleton)){
> self::$_singleton = new PDO_Singleton;
> }
> return self::$_singleton;
> }
> }
>
>
> $dbh = PDO_Singleton::getInstance();
>
>
>
> OK, So now I have my instance and i want to use my PDO object, if try
> this:
>
> $statemant = $dbh->_connection->prepare("select * from pans");
>
> Which obviously gives me the message "Cannot access private property
> PDO_Singleton::$_connection" because $_connection is declared private.
>
>
> So how am I supposed to use my PDO object without having to declare it
> public or access it from within my PDO_Singleton class?
>
>
> Help appreciated,
>
> Regards,
>
> Paul
>

Why not use inheritance?

class PDO_Singleton extends PDO {
private static $_singleton;

public static function getInstance()
{
if (is_null (self::$_singleton)){
self::$_singleton = new PDO_Singleton;
}
return self::$_singleton;
}
}

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
jstucklex@attglobal.net
==================

Re: Accessing a PDO DB object from within Singleton pattern

am 03.10.2007 00:25:51 von Macca

Thanks guys.

I wouldnt have thought of using inheritance.


Regards,

Paul