troubleshooting Question

How does this Singleton Class work?

Avatar of Bruce Gust
Bruce GustFlag for United States of America asked on
5 Comments1 Solution101 ViewsLast Modified:
I've been able to successfully incorporate a Singleton class into my app and am now using that to establish my database connection.

But while it's working, I want to know WHY it works. Below is a breakdown of what I understand combined with some questions. If you could "fill in the gaps," that would be awesome!

First of all, I'm going to use the Scople Resolution Operator to call what I know to be a static method from the "DB" class tht is also protected...

$display_page=DB::query("SELECT * from employers where id=?", [$id]);

Here's the private static function that's beeing called:

private static function query($statement, $bindParams=NULL, $assoc=NULL)

 Because its private, the, __callStatic method is going to be called which looks like this:
public static function __callStatic($name, $args=NULL){
            // connect here

			self::$conn = new PDO("mysql:dbname=adsf; host=adsf", "adsf", 'adsf');
			self::$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        return call_user_func_array('self::'.$name, $args);
But here's where I'm getting confused.

If I understand things correctly, "$name, $args=NULL" is my query ("SELECT * from employers were id=?") and my [$id], correct?

...and then when you get to return call_user_func_array('self::'.$name, $args), 'self::'.$name is my $statement and the $args is my [$id'] value.

So how does $conn get included / implemented?

From what I understand, __calStatic is going to be called automatically when "private static function query" is called. I see how the "($name, $args=NULL)" dynamic places those two entities in a spot where they can be acted upon, but how does $conn get established?

Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros