I have no idea if I'm doing this efficiently, or ideally, but I've written what I think is a factory. Please help me understand if this is good practice, or if there is another direction I should take this. There's no particular project in mind with this, I'm just farting around. (Written in PHP)
class Factory
{
private function __construct(){}
/**
* @param $obj Object to Create.
* @param $params Params (use array for multiple parameters) to be passed.
*/
public static function build($obj, $params=null) {
switch ($obj) {
case 'db': return new DB($params);
default: return null;
}
}
}
This lets me do something like $db = Factory::build("db","test_database_name");
The DB class just builds a PDO connection using appropriate parameters based on the database I'm trying to access, which is useful for me because I deal with MySQL and MS SQL all day long, and I don't want to have to remember all the connection strings, users, and passwords (stored in the DB class). That's kind of besides the point, I just intend this to be expanded as I develop more useful classes, so...
I just want to get some opinions on the the topic of a Factory. Is this a good method? are there others that are considered superior? If so, what have I overlooked?