#36930 [Bgs]: Calling is_array on ArrayObject problem (even when casting)
| From: | jausions@php.net | Date: | Mon, 20 Aug 2007 16:05:40 +0000 |
| Subject: | #36930 [Bgs]: Calling is_array on ArrayObject problem (even when casting) | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-116373@lists.php.net to get a copy of this message | ||
ID: 36930
Updated by: jausions@php.net
Reported By: ante at abstraktmedia dot com
Status: Bogus
Bug Type: SPL related
Operating System: Win XP
PHP Version: 5.1.2
New Comment:
It would be nice though to add an option to the is_array() function to
allow extended checking on the variable passed and whether to consider
ArrayObject (or alike) as regular PHP arrays.
$trueArray = array();
$arrayObject = new ArrayObject(); // or class implementing same
interfaces as ArrayObjects
is_array($trueArray); // => TRUE
is_array($trueArray, true); // => TRUE
is_array($trueArray, false); // => TRUE
is_array($arrayObject); // => FALSE
is_array($arrayObject, true); // => TRUE
is_array($arrayObject, false); // => FALSE
-Philippe
Previous Comments:
------------------------------------------------------------------------
[2006-03-31 12:02:38] ante at abstraktmedia dot com
As you wish Mike but this is just very very bad workaround....this
checking should be done internaly not on user end...
------------------------------------------------------------------------
[2006-03-31 11:56:21] mike@php.net
Thank you for taking the time to write to us, but this is not
a bug. Please double-check the documentation available at
http://www.php.net/manual/ and the instructions on how to
report
a bug at http://bugs.php.net/how-to-report.php
if (($foo instanceof ArrayObject) || is_array($foo))
------------------------------------------------------------------------
[2006-03-31 11:46:51] ante at abstraktmedia dot com
Description:
------------
OK...
I'm using ArrayObject quite a lot and I experienced something which is
not logical...
If I have
$arrayObj = new ArrayObject(Array("Test"))
function is_array() returns false which is not ok in the first
place...but never mind...we'll cast it to array so we use
if(is_array((array) $arrayObj)) .....
but the problem is what if $arrayObj is not Array...what if it's null
or string? casting to array will make the function
return true even if the variable is null....
That's the real problem....
Shor story...I cannot use is_array on ArrayObject if I don't cast that
object to a "normal" array..But if I use casting and variable is for
example string then is_array will return true when it should return
false....
Reproduce code:
---------------
Logicaly:
$arrayObj = new ArrayObject(Array("Test"));
//we don't use casting!!
if(is_array($arrayObj)) ... should return TRUE
but because of the internal workings of PHP we have to convert
ArrayObject to Array before sending it to is_array function...
But what if our Array is String actually...??or Null???
Because we HAVE TO cast that ArrayObject before sending it to is_array
it will also cast String or anything else to an Array and therefore
function will return TRUE...
$arrayObj = new ArrayObject(Array("Test"));
//$arrayObj = null;
if(is_array((array) $arrayObj)) {
print("Is array!");
}
else {
print("Not array!");
}
Now try uncommeting second line and commenting first...
------------------------------------------------------------------------
--
Edit this bug report at http://bugs.php.net/?id=36930&edit=1