Implicit Evaluation with PHP

14 September 2006

Objects and Arrays in PHP

In PHP, Objects and Arrays are very similar. Objects can easily be cast as an array with get_object_vars, and a simple foreach loop can easily construct an stdObject from an array. Their similarities mean there is little you can do with one that you cannot do with the other. Some of the differences (at first glace) are:

  1. objects can be typed with a name (the class name).
  2. Objects can have functions, arrays cannot.
  3. By virtue of functions, objects can have constructors and destructors
  4. Objects can more easily have default values
  5. Objects (or more specifically, classes) can have static methods

In truth, however, only the name and static methods are truly exclusive to objects. And what’s more interesting is that both real differences apply to classes, not objects. In PHP3, classes were just glorified arrays with special syntax. In PHP4, they became distinct but still very similar (witness all the complaints about PHP4’s object model). In PHP5, Objects became real objects comparable to other languages. At the point that PHP internally differentiated between objects and arrays (PHP4), a method called get_class came along to give meta information about that class. That is information an array simply cannot have. And so there is the first difference.

Arrays actually can have functions. Think about the similarities of functions and variables in objects:

$frank = new Person;
$frank->name = "Frank";
$frank->age();

The only difference is the the parentheses. And in fact, consider

$frank = array (
"name" => "Frank",
"iAge" => 0,
"age" => create_function (
'&$person',
'$person["iAge"]++;'));
$frank["age"]($frank);

Though it’s unfortunate you must call $frank twice, it is an array with a function in it. It is because $this does not exist within a create_function lambda. But merely trying this exercise illustrates something closer to how objects actually work. Scopes, name spaces and classes are all nice things. But when they hit silicon, the class functions are just ordinary functions with new names. For example $frank->age() in OOP is $frank = person_age ($frank) in object-oriented imperative code.

The constructor/destructor argument is valid. There is no way to have either strictly using arrays. If, however, the call to array() is abstracted into a function, constructor and destructor functions that return and destroy the array can serve the same purpose.

Default values can be arranged by making a template array and cloning that for instanciations. Observe:

$classPerson = array ("name" => "", "age" => 21);
$frank = $classPerson; // adjust for cloning depending on PHP version

Frank and all other instanciations pick up the values $classPerson defined.

Static functions are also absent in arrays, in a technical sense. However, they can be put into a template array and accessed conventionally.

There is no legitimate reason I can think of that you would do any of this. OOP exists to abstract this kind of development away. However, this does illustrate the similarities and how able PHP arrays are compared to objects.

One Comment currently posted.

Peter Goodman says:

In PHP5, you can use the SPL ArrayObject with implements ArrayAccess, Countable, and IteratorAggregate so that the array object can be used as either an array/object, looped in a foreach like iterators, and counted using sizeof() or count().

Post a comment on this entry: