Here’s a quick question for the wider PHP programming community … if you’re writing code that tests for the presence of an array, should is_array() also accept objects that behave like arrays?

$testObject = new ArrayObject(array('Tom'));
$testResult = is_array($testObject);

On PHP 5.3.2, $testResult gets set to false. If you wanted to get an object that behaves like an array past this sort of test, you’d have to write:

$testObject = new ArrayObject(array('Tom'));
$testResult = (is_array($testObject) || $testObject instanceof ArrayAccess);

I’m just curious as to what other PHP programmers think of this. Having to write the second test is longer, and there seems to be little or no benefit gained by having to do so.

What do you think?

Comments are closed.