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();
.
.
.
}
There are a couple of things that i dont get
!db
doConnectionFactory::getFactory()->getConnection();
getFactory
method 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