[development] Drupal - to use objects or arrays
AjK
drupal at f2s.com
Wed Jan 10 08:58:08 UTC 2007
> A single entity is an object. (Eg, nodes, users, etc.) A list
> of things is
> an array. A list of entities is an array of objects.
If only that were true. I'll give you an example of where it isn't true. I once created a "directory of people" for a client. I created a node type to hold the records so tax could be used. I created a form builder function and a db update function to handle it. All's well. Then the client says "some of the people in the directory are actual site users, would be nice if those users could be joined to their record so they can edit their own entry". Made sense, so I used a tab in "my account" to allow for "per user" editing and I initially tried to reuse my existing form builder and db update functions. Guess what? When the form is submitted on "edit node" the update function gets an object, when it's submitted via drupal_get_form() it's an array. Solution, I just cast the array to object in the db update function. But it demonstrates where Drupal is not consistent in it's use of data type containers. Is it an array or an object? Who knows unless you test for it.
> where it really doesn't make a difference, using
> db_fetch_object() seems more
> common than db_fetch_array(), simply because there's fewer funky
> characters
> involved with objects than arrays. :-)
Yes, I see this. fetch_object is slower than fetch_array. Not by much granted, but given we're always looking at performance issues and the speed of Drupal as a whole I'm left wondering why developers are left to use less funky characters over performance. Imho, use an object when you need the functionality of an object, otherwise use arrays. Since Drupal has chosen (at this time) not to use classes I'm surprised the -> operator exists at all anywhere, but it's everywhere!
I've discussed this on IRC before (changing objects to arrays) but it's probably a mega change too far, at this time at least ;)
As Rowan says: "I'd be happy to see objects removed completely from Drupal..."
+1 ;)
And Larry, sorry, I'm not looking to open a can of worms here (that's why I said mega change to far) but I just want to point out that if a page makes 100's of SQL queries and they are all served by db_fetch_object() over db_fetch_array() then you are effectively burdening the performance of your application in favour of "having a nice semantic sense" for the developer. If performance wasn't an issue I would give two hoots or care less. But performance IS an issue so it's a valid "can of worms".
"personally find that it is easier to say $foo->$bar['baz'] rather than $foo['bar']['bar]'."
Exactly what I'm saying. To allow for ease of writing you're adding cpu cycles (not to mention confusion as noted at the beginning of my email, "is it an array or an object? Dunno, must check it, more cpu cycles). Remember, generally you write it once, computers run it millions and millions (if not billions) of times. For whose benefit should it be written, the developers tastes because it looks nice or easier to write or the target hardware where it's actually going to run?
Mind you, having said all that, those extra cycles pale into insignificance when you count the number of fstat() calls ;) You can probably tell I come from a coding environment where every cycle counts (machine vision). </rant>
--Andy (AjK)
More information about the development
mailing list