Drupal and CakePHP - Why so many arrays?

As a relative newbie to both Drupal and CakePHP, I see the same programming style in both applications, and I'll call this style "programming in arrays of arrays". That is, anywhere I would normally use an object in Java, the Drupal and CakePHP developers have used an "arrays of arrays" to store data.

Let's look at some source code examples so I can compare their "arrays of arrays" approach to an OOP approach.

A PHP "arrays of arrays" source code example

To explain this, here's some PHP source code that you might find in Drupal or CakePHP:


$people['al'] = array(
  'first_name' => 'Alvin',
  'last_name' => 'Alexander'

$people['barney'] = array(
  'first_name' => 'Barney',
  'last_name' => 'Rubble',
  'children' => array('Bam-Bam')

$people['fred'] = array(
  'first_name' => 'Fred',
  'last_name' => 'Flinstone',
  'children' => array('Pebbles')

foreach ($people as $value) {


As you can see, each 'person' in the $people array is declared as an array, and each element in that array may point to another array, and so on. The concept of a 'person' is never specifically declared anywhere, it is just understood to be represented by this "arrays of arrays".

A PHP object-oriented approach

Coming to PHP from Java, I find this a little confusing, and it took me a while to understand that what they are doing could also be represented in an object-oriented manner, like this:


  class Person
    var $first_name;
    var $last_name;
    var $children;

    function Person($fname, $lname, $children)
      $this->first_name = $fname;
      $this->last_name = $lname;
      $this->children = $children;

  $people = array();
  $al = new Person('Alvin', 'Alexander', null);
  $bambam = new Person('Bam Bam', 'Rubble', null);
  $pebbles = new Person('Pebbles', 'Flinstone', null);
  $barney = new Person('Barney', 'Rubble', array($bambam));
  $fred = new Person('Fred', 'Flinstone', array($pebbles));

  array_push($people, $al);
  array_push($people, $barney);
  array_push($people, $fred);

  foreach ($people as $value) {


(That code may have a few errors in it, I haven't tested it, but it looks right. :)

To me, this code is much cleaner, and easier to read and understand than the "array of arrays" approach. Additionally, you can add "behaviors" to the Person class here that you can't add with the "arrays of arrays" approach.

PHP "arrays of arrays" discussion

Now, I'm not writing this to say that what they've done is "wrong". There are many ways to write programs, and the fact that both of these tools have taken the "arrays of arrays" programming style seems like they're trying to tell me something ... I'm just not sure what. Their style is much closer to a C programming style with structs, but they don't even use structs (structures).

Were these decisions made long ago, when PHP didn't really support classes and objects? Is there a problem with using classes and objects in PHP? Does PHP not support reflection?

Again, as a relative PHP, Drupal, and CakePHP newbie, I can only guess right now ... as I try to wrap my head around this "arrays of arrays" programming approach.



That is only one issue with Drupal. You almost have to unlearn normal programming ways to use it. Someone could right a translation guide for programmers; normal way vs drupal way

With that said, I am making my living using it and continually banging my head against a wall while programming in it.

Add new comment

Anonymous format

  • Allowed HTML tags: <em> <strong> <cite> <code> <ul type> <ol start type> <li> <pre>
  • Lines and paragraphs break automatically.