473,395 Members | 1,676 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Accessing a PDO DB object from within Singleton pattern

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

Oct 1 '07 #1
4 3589
On Oct 1, 1:49 pm, macca <ptmcna...@googlemail.comwrote:
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.

Oct 1 '07 #2
..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
Oct 2 '07 #3
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.
js*******@attglobal.net
==================
Oct 2 '07 #4
Thanks guys.

I wouldnt have thought of using inheritance.
Regards,

Paul

Oct 2 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Ryan Mitchley | last post by:
Hi all I have code for an object factory, heavily based on an article by Jim Hyslop (although I've made minor modifications). The factory was working fine using g++, but since switching to the...
3
by: John Ratliff | last post by:
When I dereference a pointer, does it make a copy of the object? Say I had a singleton, and wanted an static method to retrieve it from the class. class foo { private: static foo *bar; ...
8
by: Mark Neilson | last post by:
1. What is the best way to make a single instance of my top level class (DLL) internally available to all other members of the assembly? The top level object is where all other access is made in...
8
by: Marty | last post by:
Hi, I'm new to C#, I used to code in VB.NET. Where is the best place to declare all my constants and global objects in my C# project to have them accessible globally? I have an event logger...
16
by: Elad | last post by:
Hi, I have an application that is made up of several executables. I need all these executables to use the same instance of an object. What is the best, most efficient way to approach this? ...
5
by: Christopher D. Wiederspan | last post by:
This is a bit tough to figure out the best way to ask, but here it goes. I've got a class, say MyObject, and everytime this class is instanciated, I actually want to get a reference to an existing...
5
by: Rich | last post by:
The following code produced a singleton object with application scope when it should have had page scope: public class Singleton { private static Singleton uniqueInstance = null; private...
5
by: Sam.Gundry | last post by:
Hi, I wish to share an object through a bunch of functions without declaring it globally. I have achieved this through the following function: VideoReceiver* getReceiver() { static...
12
by: titan nyquist | last post by:
I have a class with data and methods that use it. Everything is contained perfectly THE PROBLEM: A separate thread has to call a method in the current instantiation of this class. There is...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.