Your code seems to do what you want, but maybe we can use fewer objects using inheritance, and maybe we can avoid static properties in non-deterministic classes.
Also regarding the use of a dependency injection pattern that is capable of handling multiple compounds but supports the use of one instance of it. first of all, classes after
$params = array ('host'=>'localhost', 'db'=>'ice', 'user'=>'kopitar', 'pass'=>'topnet', 'charset'=>'utf8');
we can either pass $db to our functions
$location_names = Location::getLocationNames($db);
or all $ handle. until $ handle is restored, it will always return the same database connection.
$location_names = Location::getLocationNames($handle);
if i want to restore i need all $handle
$handle->__construct(); $db2 = $handle->getInstance();
Regarding classes, I think we want params to come from an instanciated class, so we can change them later.
class db { function __construct($params) { foreach ($params as $param => $value) { $this->{$param} = $value; // assigns the connections infos } } protected function connect() { $dsn = 'mysql:host='.$this->host.';dbname='.$this->db.';charset='.$this->charset; return new PDO($dsn,$this->user,$this->pass); } }
factory creates a connection from the parameters and passes it to something else, a good factory
class factory extends db { protected function create() { return $this->connect(); } }
now we want our object to keep in touch until we rebuild it. therefore we give an instance of it
class instance extends factory { function instantiate() { $this->instance = $this->create(); } }
and, just as importantly, our handle that returns the instance. he may be in a classroom class ...
but I feel four and I find no real reason not to do this.
class handle extends instance { function __construct($params) { db::__construct($params); $this->instantiate();