tags:

views:

352

answers:

6

I'm building a MySQL query "interface". It will allow me to make selects more easily. However, i'm having a doubt about passing empty parameters. Some of my function parameters are not required, so, i'm passing them empty.

For example: query('table', '*', '', '', '0,10');

Being: query($table, $from, $where, $orderby, $limit)

Is this against some best practice? Am i doing it wrong?

A: 

Try it like this:

$query = new Query();
$query->setTable($table);
$query->setFrom($from);
$query->execute();

That way you can basically have named and/or optional parameters, even though PHP doesn't support them.

Skilldrick
But my class has other functions, it's not only a query class. It has functions for creating tables and etc.
Vinny
My class is called Database. And it handles database connection as well. So, it works like this: $db = new Database($db, $user, $pass); $db->query('table', '*', '', '', '0,10');
Vinny
@Vinny: Create another class just for the queries, so you can create query ojects.
tharkun
Should the Query class extend the DB class, for example?
Vinny
No, they are not related. The query class builds strings. DB class connects to a database.
MooGoo
The query class definitely does not extend the db class. There is no gain in that. The query oject just helps you to handle the build-up of the query more easily and does not need to know about the DB object.
tharkun
Then, how do i connect to the DB inside the Query class?
Vinny
My Database() class connects to the DB using PDO, inside the __constructor function.
Vinny
You do not connect to the DB inside the query class. The query class just builds the query, which is then used by the DB clas.
tharkun
Erm... how? I want the query class to build the query and return the results of it.
Vinny
The query object only builds the query, the query is executed by the DB object, which is the object in need of the result.
tharkun
I'm not sure if i follow. I would run new Database(), connect to the DB, run new Query() and build it... but what now?
Vinny
The DB class would have a `query` method that would simply take a SQL statement string and execute it. Something like `$query = new Query(); /* build query */ $result = $db->query($query);`
MooGoo
+1  A: 

I would set the default arguments in the function itself, so you can control if is a default value to just ignore it... like this...

query($table, $from=null, $where=1, $orderby=null, $limit=10)

Bladedu
+3  A: 

If you want to build this yourself because it is your task or because you want to do it for the sake of learning, go ahead. Otherwise don't do it yourself, that's exactly what frameworks are for. Zend_Db (Zend Framework) for example, does this all very neatly.

If you want to have optional parameters, you have to define default values in the function. The optional parameters have to follow after the non-optinal ones.

An even better (more flexible, better maintainability) approach would be to add each part of the query by its own function. Create a query class and create methods to add all the different parts of the query to the query object.

tharkun
I want to learn OOP in PHP, that's why i'm working on this. I'm building a blog system following the OOP practices and patterns.
Vinny
@Vinny: Then you should follow the "Query Class" idea. I think it's pretty neat solution.
Night Shade
A: 

a seperate Query Builder class seems the best way to go, I have used Doctrine ORM for a while and it also had a seperate query builder.

But if this isn't for learning purposes I highly suggest using an existing solution as Doctrine or Zend_Db (I have only used doctrine however).

Xeross
+2  A: 

if you want a real OOP interface, you'd better to get familiar with Doctrine.

$q = Doctrine_Query::create()
    ->select('u.id')
    ->from('User u')
    ->whereIn('u.id', array(1, 3, 4, 5));

echo $q->getSqlQuery();

(example snipped from the other recent question)

Doctrine query builder will let you avoid empty parameters as well as procedural style (which, to be honest, has nothing to do with your OOP intentions) and many more things to use and to learn from.

Col. Shrapnel
A: 

If you are trying to learn OOP, then try PHP Data Object - PDO. It provides a consistent OOP interface for accessing databases from PHP. It is already defined as an extension for PHP, so you better try to learn and use it rather than inventing the wheel again.

But if you still insist on making an Object Oriented Database Access layer, then you should follow this answer. I think it is pretty neat.

Night Shade