Bug #45595 [Com]: ArrayObject::STD_PROP_LIST does not behave as documented with foreach
| From: | elloromtz at gmail dot com | Date: | Tue, 23 Apr 2013 02:59:57 +0000 |
| Subject: | Bug #45595 [Com]: ArrayObject::STD_PROP_LIST does not behave as documented with foreach | ||
| References: | 1 | Groups: | php.bugs |
| Request: | Send a blank email to php-bugs+get-178586@lists.php.net to get a copy of this message | ||
Edit report at https://bugs.php.net/bug.php?id=45595&edit=1
ID: 45595
Comment by: elloromtz at gmail dot com
Reported by: robin_fernandes at uk dot ibm dot com
Summary: ArrayObject::STD_PROP_LIST does not behave as
documented with foreach
Status: Not a bug
Type: Bug
Package: SPL related
Operating System: *
PHP Version: 5.2CVS-2008-07-22 (snap)
Assigned To: colder
Block user comment: N
Private report: N
New Comment:
ArrayObject::STD_PROP_LIST seems to work in scattered versions of 5.3.x
I have confirmed it working for 5.3.21 but it does NOT work for 5.3.23 nor 5.3.15
By the way, how exactly isn't this a bug? Is it intentional for it to work in
scattered versions of 5.3.x?
If so the documentation should reflect this
Previous Comments:
------------------------------------------------------------------------
[2013-01-26 08:11:05] daniphp at gmail dot com
But the later example proves that:
"The ArrayObject::STD_PROP_LIST doesn't seem to alter the behaviour of
ArrayObject in any noticeable way"
so the PHP documentation is wrong ?
------------------------------------------------------------------------
[2012-11-16 18:39:34] levim@php.net
This is not a bug, as explained by rquadling at php dot net above.
------------------------------------------------------------------------
[2011-02-25 12:00:34] rquadling@php.net
The ArrayObject::STD_PROP_LIST doesn't seem to alter the behaviour of ArrayObject in any
noticeable way. Setting properties on an instance of ArrayObject at runtime adds the properties to
the instance, rather than storing them as key/value pairs in the internal array. $o->prop = val
is a property with or without this flag and cannot be accessed via $o['prop']
It would seem ArrayObject::ARRAY_AS_PROPS is what turns properties into key/value pairs and allows
you to access the key/value pairs as instance properties. $o->prop = val is the same as
$o['prop'] = val.
<?php
error_reporting(-1);
$a = array('first' => 'a');
// Build objects
$o1 = new ArrayObject($a );
$o2 = new ArrayObject($a, ArrayObject::STD_PROP_LIST );
$o3 = new ArrayObject($a, ArrayObject::ARRAY_AS_PROPS);
$o4 = new ArrayObject($a, ArrayObject::STD_PROP_LIST | ArrayObject::ARRAY_AS_PROPS);
$ao = array($o1, $o2, $o3, $o4);
foreach($ao as $o) {
// Add a property.
$o->last = 'z';
// Show flags, foreach() and properties.
echo
'Flags set to ', str_pad(decbin($o->getFlags()), 2, '0', STR_PAD_LEFT),
PHP_EOL,
print_r($o, True), PHP_EOL,
'Foreach:', PHP_EOL;
foreach($o as $k => $v) {
echo " $k => $v", PHP_EOL;
}
echo
'Properties:', PHP_EOL,
" first => {$o->first}", PHP_EOL,
" last => {$o->last}", PHP_EOL,
PHP_EOL;
}
?>
outputs (using 5.3+) ...
Flags set to 00
ArrayObject Object
(
[last] => z
[storage:ArrayObject:private] => Array
(
[first] => a
)
)
Foreach:
first => a
Properties:
Notice: Undefined property: ArrayObject::$first in Z:\ar1.php on line 29
first =>
last => z
Flags set to 01
ArrayObject Object
(
[last] => z
[storage:ArrayObject:private] => Array
(
[first] => a
)
)
Foreach:
first => a
Properties:
Notice: Undefined property: ArrayObject::$first in Z:\ar1.php on line 29
first =>
last => z
Flags set to 10
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[first] => a
[last] => z
)
)
Foreach:
first => a
last => z
Properties:
first => a
last => z
Flags set to 11
ArrayObject Object
(
[storage:ArrayObject:private] => Array
(
[first] => a
[last] => z
)
)
Foreach:
first => a
last => z
Properties:
first => a
last => z
------------------------------------------------------------------------
[2008-10-24 15:56:27] jani@php.net
Assigned to the SPL maintainer.
------------------------------------------------------------------------
[2008-07-22 17:02:41] robin_fernandes at uk dot ibm dot com
Description:
------------
The SPL documentation states that if the ArrayObject::STD_PROP_LIST flag is set on an ArrayObject
instance:
"Properties of the object have their normal functionality when accessed as list (var_dump,
foreach, etc.)"
(see http://www.php.net/~helly/php/ext/spl/classArrayObject.html#daebe26f8478746da33c266a730714a9
)
This flag does affect var_dump(), but it seems to have no impact on foreach. See reproduce code.
I'm not sure whether this is a functional problem or a documentation problem.
Reproduce code:
---------------
<?php
// ArrayObject::STD_PROP_LIST:
// Properties of the object have their normal functionality when accessed as list (var_dump,
foreach, etc.)
echo "Create instance of ArrayObject and add some normal properties...\n";
$ao = new ArrayObject(array('x', 'y', 'z'),
ArrayObject::STD_PROP_LIST);
$ao->p1 = 1;
$ao->p2 = 2;
$ao->p3 = 3;
echo "\nGet property list with var_dump:\n";
var_dump($ao);
echo "\nGet property list with foreach:\n";
foreach ($ao as $key=>$value) {
echo " $key=>$value\n";
}
?>
Expected result:
----------------
Create instance of ArrayObject and add some normal properties...
Get property list with var_dump:
object(ArrayObject)#1 (3) {
["p1"]=>
int(1)
["p2"]=>
int(2)
["p3"]=>
int(3)
}
Get property list with foreach:
p1=>1
p2=>2
p3=>3
Actual result:
--------------
Create instance of ArrayObject and add some normal properties...
Get property list with var_dump:
object(ArrayObject)#1 (3) {
["p1"]=>
int(1)
["p2"]=>
int(2)
["p3"]=>
int(3)
}
Get property list with foreach:
0=>x
1=>y
2=>z
------------------------------------------------------------------------
--
Edit this bug report at https://bugs.php.net/bug.php?id=45595&edit=1