4
class ConnectionFactory
{
    private static $factory;
    public static function getFactory()
    {
        if (!self::$factory)
            self::$factory = new ConnectionFactory(...);
        return self::$factory;
    }

    private $db;

    public function getConnection() {
        if (!$db)
            $db = new PDO(...);
        return $db;
    }
}

function getSomething()
{
    $conn = ConnectionFactory::getFactory()->getConnection();
    .
    .
    .
}

Source

There are a couple of things that i dont get

  1. Am i right when i say "staic property of a class can be accessed without initiating the class"
  2. what does !db do
  3. How is this happening ConnectionFactory::getFactory()->getConnection();
  4. Could somebody explain getFactory method
Community
  • 1
  • 1
aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242

4 Answers4

3
  1. you are right here.
  2. ! is a NOT. Which means here that if $db is false then initialise it. Since its in a static method it will stay initialised and next time the existiong $db will be returned since this second time !$db == false.
  3. like for $db it is checking if an instance of $factory exists, if not create one and returne it, otherwise return the existing one.

4.

public static function getFactory()
{
     if (!self::$factory) // is self::$factory initialised?
            self::$factory = new ConnectionFactory(...); //initialise it
     return self::$factory; //return self::$factory
}

Also here $factory seems to be a variable that is set somewhere eles. Presumably it could contain a couple of different class names. Does not change anything to the way the function works. Its a singleton pattern

Adding a interesting link about this pattern wikipedia

aWebDeveloper
  • 36,687
  • 39
  • 170
  • 242
Iznogood
  • 12,447
  • 3
  • 26
  • 44
  • thanks and i know what ! is.... what do u mean by "$db is false then initialise" when will it be false ( i know in initial time but what happens behind the scene) – aWebDeveloper Mar 09 '12 at 17:23
  • Exactly it will only be false on the first call. Simple as that. Behind the scene it just creates a variable in memory and stores it. next time it just returns this object. – Iznogood Mar 09 '12 at 17:25
  • does it mean that the first time the variable will not be in memory (&/or/i.e not yet created) – aWebDeveloper Mar 09 '12 at 17:29
  • Exactly. It is only created when you first need it. After that it stays. – Iznogood Mar 09 '12 at 17:33
  • so one final this the above code allows me to have 2 db connections with different details but only 1 instance of each – aWebDeveloper Mar 09 '12 at 18:05
1

For 1. yes, a static property of a class is always accessible using ClassName::$staticvarname

For 2. there is surely a bug here. it should be if(!$this->db) and all code in getConnection should use $this->db instead of $db.

the getFactory() is here an "alias" of the more standard getInstance() of Singleton pattern. It returns the single instance of the class if existing & create the single instance if not existing.

The getFactory()->getConnection() calls is simple: getFactory() returns the single instance of the ConnectionFactory class. So now that you have the Singleton instance, you can perform any call on it. The getConnection() returns the "single" PDO instance that is handled by the singleton (stored in $db member)

dweeves
  • 5,525
  • 22
  • 28
1
  1. Correct
  2. ! is the NOT operator. Basically if (!$db) means if $db is null then execute the if block
  3. Get factory is a function which is returning the (single) instance of ConnectionFactory, this instance has a function getConnection()
  4. In the singleton pattern only a single instance of class should exist, which is a private static member of the class itself. The getFactory() function is the way to get a reference to this single instance (it first initializes the instance if it is null).
Umair
  • 3,063
  • 1
  • 29
  • 50
1
  1. Yes, static denotes that it will be the same value for every object that requests it, so no instantiation is needed
  2. ! means NOT. It is most often seen as !=, denoting NOT EQUAL. In this case, it is being checked to find if the db object has been created, so it means db is NOT NULL.

3 & 4. The method that you specified is saying to use the class (without instantiating it), and call getFactory(). Which will create the factor if it is NOT NULL, otherwise, it will return the already created factory. After that, it requests the connection using the instance of the factory that was returned. Since there is only ever one factory, then we can assume that once the connection has been instantiated (in the same way that the factory was instantiated), then that connection will be available for all uses of factory moving forward

I suggest reading the wikipedia article about Singleton patterns. Hopefully, this can be useful. Also, keep in mind, generally speaking the singleton pattern has been deemed more of an anti-pattern that should be avoided. This class seems to me that it could just as easily have been created as a static class with a static method of getConnection. With the code displayed, it seems that the creation of itself is pointless here. But, that is just in this small context.

Justin Pihony
  • 66,056
  • 18
  • 147
  • 180