#34934 [Fbk->Opn]: offsetExists not called in derived class

2005-10-20 Thread adove at booyahnetworks dot com
 ID:   34934
 User updated by:  adove at booyahnetworks dot com
 Reported By:  adove at booyahnetworks dot com
-Status:   Feedback
+Status:   Open
 Bug Type: SPL related
 Operating System: WinXP, Linux
 PHP Version:  5.0.5
 New Comment:

Here's a full code sample to replicate. Note I stripped out the method
comments. If you want them I can repost.

recurs($mValue);
}
elseif(is_object($mValue) && ($mValue instanceof ArrayObject))
{
// Get the raw array from our ArrayObject for handling.
$mValue = &$this->recurs($mValue->getArrayCopy(),
$constFlags);
}
elseif(null == $mValue)
{
$mValue = array();
}
elseif(!is_object($mValue))
{
// Turn primative into an indexed array.
$mValue = array($mValue);
}
// else - it's an object, enum its public properites.

parent::__construct($mValue);
}

protected function explodeKeyPath($mKeyPath)
{
return explode("/", trim($mKeyPath, "/"));
}

function offsetSet($mKey, $mValue) 
{
$mKey = trim($mKey, "/");
if(stristr($mKey, "/"))
{
// A path was requested. Start out pointing to ourself.
$mRetval = $this;

$aKeyPathParts = $this->explodeKeyPath($mKey);

// Need to take the LAST path off. That's what we assign
our value too. 
$strLastPathPart = array_pop($aKeyPathParts);

foreach($aKeyPathParts as $strPathPart)
{
// We know we our data are either a primative type or an
ArrayAccess. 
if(($mRetval instanceof ArrayAccess) && 
array_key_exists($strPathPart, $mRetval)
)
{
$mRetval = $mRetval[$strPathPart];
}
else
{
// Need a new empty one of us and then point to
it!
$mRetval[$strPathPart] = 
$this->newArrayObject($strPathPart, array(),
$this->getFlags());
$mRetval = $mRetval[$strPathPart];
}
}

// Now put our value!
$mRetval[$strLastPathPart] = $mValue;
}
else
{
if(is_array($mValue))
{
// Need to create a new instance of us!
$mValue = $this->newArrayObject($mKey, $mValue,
$this->getFlags());
}

parent::offsetSet($mKey, $mValue);
}
}

function offsetGet($mKey) 
{ 
$mRetval = null;

$mKey = trim($mKey, "/");
if(stristr($mKey, "/"))
{
// A path was requested. Start out pointing to ourself.
$mRetval = $this;

$aKeyPathParts = $this->explodeKeyPath($mKey);

foreach($aKeyPathParts as $strPathPart)
{
// We know we our data are either a primative type or an
ArrayAccess. 
if(($mRetval instanceof ArrayAccess) && 
array_key_exists($strPathPart, $mRetval)
)
{
$mRetval = $mRetval[$strPathPart];
}
else
{
$mRetval = null;

break;
}
}
}
else
{
$mRetval = parent::offsetGet($mKey);
}

return $mRetval; 
} 

function offsetUnset($mKey) 
{ 
$mKey = trim($mKey, "/");
if(stristr($mKey, "/"))
{
// A path was requested. Start out pointing to ourself.
$oArray = $this;

$aKeyPathParts = $this->explodeKeyPath($mKey);

// Need to take the LAST path off. That's what we unset. 
$strLastPathPart = array_pop($aKeyPathParts);

foreach($aKeyPathParts as $strPathPart)
{
// We know we our data are either a primative type or an
ArrayAccess. 
if(($oArray instanceof ArrayAccess) && 
array_key_exists($strPathPart, $oArray)
)
{
$oArray = $oArray[$strPathPart];
}
else
{
// H... bad path... Die. Nothing will happen
below.
break;
}
}


#34934 [Fbk->Opn]: offsetExists not called in derived class

2005-10-20 Thread adove at booyahnetworks dot com
 ID:   34934
 User updated by:  adove at booyahnetworks dot com
-Summary:  offsetExists not called in dreived class
 Reported By:  adove at booyahnetworks dot com
-Status:   Feedback
+Status:   Open
 Bug Type: SPL related
 Operating System: WinXP, Linux
 PHP Version:  5.0.5
 New Comment:

Same behavior in both the latest 5.0.5 and 5.1...


Previous Comments:


[2005-10-20 18:57:25] [EMAIL PROTECTED]

Please try using this CVS snapshot:

  http://snaps.php.net/php5-latest.tar.gz
 
For Windows:
 
  http://snaps.php.net/win32/php5-win32-latest.zip





[2005-10-20 18:52:00] adove at booyahnetworks dot com

Description:

I've searched the bug list and found only one bug that hints arond the
same issue (http://bugs.php.net/bug.php?id=34849) BUT it appears the
closing statement in the bug "ArrayAccess objects don't work in
array_*() functions. You may want to
turn this report into a feature request?" seems incorrect per the code
below. 

I have an class (MyArray) I derived from ArrayObject and have
overridden the ArrayAccess::offset* methods to allow me to make the
array addressable via an XPath-styled key path, e.g., /key1/key2,
etc.). All of the offset* methods get called just fine and work like a
champ EXCEPT offsetExists... 

Based on the bug ref'd above, offsetExists is just an adjunct function
on the ArrayAccess interface and not "hooked up" to any language
constructs. HOWEVER, based on emperical information, this appears
incorrect/inconsistent. 

The code submited illustrates the point. If I take a raw ArrayObject,
set it to a multi-dim'd array and test for the root key via
array_key_exists, I get true, which is correct. Additionally, if I test
for any other key I get false, which is also correct. Based on the above
bug I would expect to get a warning about only arrays being passed to
the method blah blah. So, somehow it would *appear* that the
ArrayObject class is somehow having its offsetExists method.

When I do the same tests with my derived array object (MyArray), I get
the same results. HOWEVER, my overidden offsetExists method is never
called. This is evident via debuging (I never step into it) and in the
last test assigned to $bShouldWorkButDoesNot. This path actually does
exist and would have  been confirmed to via my offsetExists.

So, I am prepared to be given the same answer as the other bug. ;-)
BUT, I think this is inconsistent behavior so it seems a bug to me...
And, if it is not a bug, then I will 100% submit a feature request. 

Reproduce code:
---
$a = array(
"test" => array(
"one" => "dunno",
"two" => array(
"peekabo" => "do you see me?",
"anyone" => array("there")
)
)
);
$oArray = new ArrayObject($a);
$oMyArray = new MyArray($a);

$bWorks1 = array_key_exists("test", $oArray);
$bWorks2 = array_key_exists("test", $oMyArray);

$bShouldWorkButDoesNot = array_key_exists("test/two/peekabo",
$oBooyahArray);


Expected result:

true === $bWorks1
true === $bWorks2
true === $bShouldWorkButDoesNot

Actual result:
--
true === $bWorks1
true === $bWorks2
false === $bShouldWorkButDoesNot





-- 
Edit this bug report at http://bugs.php.net/?id=34934&edit=1