[development] Drupal - to use objects or arrays

Larry Garfield larry at garfieldtech.com
Wed Jan 10 03:05:29 UTC 2007


I think the general guideline is "use what makes semantic sense at the time".  
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.

When you get into really complex data structures, like FAPI, then I think 
deeply-nested arrays are easier to work with and more predictable (esp. 
between PHP 4 and PHP 5) than deeply-nested objects.  You can have an object 
one of whose properties is a complex nested array (Nodes created with CCK, 
esp. in Drupal 5, do that).  

There's no hard rule, but I think "what makes semantic sense when writing it" 
is the guideline.  Oh yes, and when iterating over records from a database 
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. :-)

On Tuesday 09 January 2007 3:57 pm, Doug Green wrote:
> To use objects or arrays?  What is the "Drupal way"?  I'd rather write code
> using objects (php's stdClass).  But I've run into a ton of problems using
> objects on php4.
>
> The CiviCRM folks have this handy guide for php5 -> php4:
>
>
>
> http://wiki.civicrm.org/confluence/display/CRM/Writing+PHP4+Compatible+code
>
>
>
> But even after using it, I'm having difficulties.  I'm starting to conclude
> that I should convert all my object references to arrays.  I only see a
> couple of object references in core, and none to anything complicated
> (arrays of objects, objects pointing to objects, object elements of arrays
> of objects, etc).
>
>
>
> Doug Green
>
> www.douggreenconsulting.com <http://www.douggreenconsulting.com/>
>
> www.civicactions.com
>
>
>
> Changing the world one node at a time!

-- 
Larry Garfield			AIM: LOLG42
larry at garfieldtech.com		ICQ: 6817012

"If nature has made any one thing less susceptible than all others of 
exclusive property, it is the action of the thinking power called an idea, 
which an individual may exclusively possess as long as he keeps it to 
himself; but the moment it is divulged, it forces itself into the possession 
of every one, and the receiver cannot dispossess himself of it."  -- Thomas 
Jefferson


More information about the development mailing list