[development] Cleaner and more consistent code.

Larry Garfield larry at garfieldtech.com
Thu Aug 2 15:45:41 UTC 2007

I forget where it was posted, but I believe Dries recently posted a count that says core uses like 80% db_fetch_object() over db_fetch_array().

The move to PHP 5 in Drupal 7, though changes the equation.  Objects become more than just alternate array syntax, and we can leverage objects as objects if it makes sense to.  (I certainly see advantages to nodes becoming real objects rather than arrays that sometimes use ->.)  That makes the "arrays are more flexible than objects" argument moot, because that's only a sane comparison to make when you're dealing with stdClass objects.  I think it's premature now to say that we ought to get rid of objects in favor of arrays, just when we're finally getting objects that aren't limited to being arrays.

The other factor is PDO.  I am planning to move the database layer to a PDO[1] backend in Drupal 7[2].  When that happens, db_fetch_*() will cease to exist.  Instead, db_query() and company will return a PDOStatement result object.  That result object will have a ->fetch()[3] method, which will always return either an object or an array, depending on the settings applied to the connection.  It will be overridable with a parameter in case you really want the other.  At present I am planning to default to object, since that's what core uses most. 

(There will also be other ->fetch*() methods, both those provided by PDO and additional ones added in a sublcass, but I'm still working on the details there.)

[1] http://us3.php.net/pdo
[2] http://www.garfieldtech.com/blog/drupal-7-database-plans
[3] http://us3.php.net/manual/en/function.PDOStatement-fetch.php

--Larry Garfield

On Thu, 2 Aug 2007 12:27:29 +0200, <ojacquet at jax.be> wrote:
> A while ago I suggested on d.o. to remove either db_fetch_row or
> db_fetch_object. This would result in cleaner code, more consistent
> function signatures and hooks. At my current work this is included in the
> coding conventions, we only use mysql_fetch_object(). I'm posting this to
> the development mailing because I am interested in what you, as
> developers,
> think about this.
> The original suggestion:
> http://drupal.org/node/158115
> Example of an inconsistent hook:
> http://drupal.org/node/124141

More information about the development mailing list