I’m writing a simple database abstraction layer in PHP. I’m currently working on query builders, classes that build the SQL “code”.
Are there any better ways for doing something like this?
if($dbengine == "MySQL")
{
MySQLQueryBuilder::buildInsert($table, $data);
}
else if($dbengine == "PgSQL")
{
PgSQLQueryBuilder::buildInsert($table, $data);
}
// And so on...
I’m thinking something like:
$querybuilder = get_class_from_name($dbengine . "QueryBuilder");
$querybuilder::buildInsert($table, $data);
Any ideas on how i can do this?
You could do like this if you want it to be static:
But I would agree with Niko comment, having instance of builder would look much nicer.
Update:
With the way as Niko suggested, you could just do:
So no
ifs required at all, code looks shorter and cleaner…