News Archive
PhpRiot Newsletter
Your Email Address:

More information

Should is_array() Accept ArrayObject?

Note: This article was originally published at Planet PHP on 20 July 2010.
Planet PHP

Here's a quick question for the wider PHP programming community a 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(aTom'));
$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(aTom'));
$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?