#36930 [Bgs]: Calling is_array on ArrayObject problem (even when casting)
| From: | ante at abstraktmedia dot com | Date: | Fri, 31 Mar 2006 10:02:39 +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-95155@lists.php.net to get a copy of this message | ||
ID: 36930
User updated by: ante at abstraktmedia dot com
Reported By: ante at abstraktmedia dot com
Status: Bogus
Bug Type: SPL related
Operating System: Win XP
PHP Version: 5.1.2
New Comment:
As you wish Mike but this is just very very bad workaround....this
checking should be done internaly not on user end...
Previous Comments:
------------------------------------------------------------------------
[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