php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #36930 Calling is_array on ArrayObject problem (even when casting)
Submitted: 2006-03-31 11:46 UTC Modified: 2007-08-20 16:05 UTC
From: ante at abstraktmedia dot com Assigned:
Status: Not a bug Package: SPL related
PHP Version: 5.1.2 OS: Win XP
Private report: No CVE-ID: None
 [2006-03-31 11:46 UTC] 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...


Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-03-31 11:56 UTC] 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 12:02 UTC] 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...
 [2007-08-20 16:05 UTC] jausions@php.net
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

 
PHP Copyright © 2001-2017 The PHP Group
All rights reserved.
Last updated: Sun Nov 19 01:31:42 2017 UTC