#38397 [Asn->Bgs]: ArrayObject not acting as object

From: Date: Sun, 13 Aug 2006 09:46:05 +0000
Subject: #38397 [Asn->Bgs]: ArrayObject not acting as object
References: 1  Groups: php.bugs 
Request: Send a blank email to php-bugs+get-100728@lists.php.net to get a copy of this message
ID: 38397 Updated by: helly@php.net Reported By: ante at novisplet dot com -Status: Assigned +Status: Bogus Bug Type: SPL related -Operating System: Win XP +Operating System: * -PHP Version: 5.2.0RC1 +PHP Version: 5.1.* Assigned To: helly New Comment: 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 This is a RTFM problem. You need to have the array in your ArrayObject be useable as properties (The name is ArrayObject not PropertyArray or something alike). So what you want to do is setting the flag ARRAY_AS_PROPS. See: http://www.php.net/~helly Previous Comments: ------------------------------------------------------------------------ [2006-08-09 10:23:43] ante at novisplet dot com <?php function print_pre($text, $ret = false) { $return = "<pre>" . print_r($text, true) . "</pre>"; if($ret) return $return; print($return); } $array = new ArrayObject(); $node1 = new ArrayObject(); $node2 = new ArrayObject(); $group = new stdClass(); $group->name = "test name"; $group->group_id = 10; $documents = new ArrayObject(); $document1 = new stdClass(); $document1->name = "Document name"; $document1->group_id = 10; $documents->append($document1); $documents->append($document1); $node1->offsetSet("group", $group); $node1->offsetSet("documents", $documents); $node2->offsetSet("group", $group); $node2->offsetSet("documents", $documents); $array->append($node1); $array->append($node2); //print_pre($array); //doesn't work foreach($array as $node) { print($node->group->name); } //works foreach($array as $node) { print($node['group']->name); } ?> ------------------------------------------------------------------------ [2006-08-09 09:46:52] tony2001@php.net Thank you for this bug report. To properly diagnose the problem, we need a short but complete example script to be able to reproduce this bug ourselves. A proper reproducing script starts with <?php and ends with ?>, is max. 10-20 lines long and does not require any external resources such as databases, etc. If the script requires a database to demonstrate the issue, please make sure it creates all necessary tables, stored procedures etc. Please avoid embedding huge scripts into the report. ------------------------------------------------------------------------ [2006-08-09 09:32:57] ante at novisplet dot com Description: ------------ If I have an array like this: ArrayObject Object ( [group] => Document_Group Object ( [_dbat] => 0 [_table] => document_group [_tableat] => document_group [_where] => [_saved] => 1 [_lasterr] => [_original] => Array ( [0] => 1 [1] => skupina 1 ) [group_id] => 1 [name] => skupina 1 ) [documents] => ArrayObject Object ( [0] => Document Object ( [_dbat] => 0 [_table] => document [_tableat] => document [_where] => [_saved] => 1 [_lasterr] => [_original] => Array ( [0] => 1 [1] => Dokument 1 [2] => [3] => 1 ) [document_id] => 1 [name] => Dokument 1 [path] => [group_id] => 1 ) [1] => Document Object ( [_dbat] => 0 [_table] => document [_tableat] => document [_where] => [_saved] => 1 [_lasterr] => [_original] => Array ( [0] => 3 [1] => Dokument 3 [2] => [3] => 1 ) [document_id] => 3 [name] => Dokument 3 [path] => [group_id] => 1 ) [2] => Document Object ( [_dbat] => 0 [_table] => document [_tableat] => document [_where] => [_saved] => 1 [_lasterr] => [_original] => Array ( [0] => 5 [1] => Dokument 5 [2] => [3] => 1 ) [document_id] => 5 [name] => Dokument 5 [path] => [group_id] => 1 ) ) ) And if I want to access group name the path would be $array->group->name but it doesn't work... I have to use "old style" $array['group']->name to get group name... Reproduce code: --------------- See description... Expected result: ---------------- Name of the group to be in $array->group->name Actual result: -------------- but it is in $array['group']->name Although everything is "ArrayObject"-ed... ------------------------------------------------------------------------ -- Edit this bug report at http://bugs.php.net/?id=38397&edit=1

« previous php.bugs (#100728) next »