|
Posted by Jerry Stuckle on 10/02/07 03:07
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
==================
[Back to original message]
|