php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #38397 ArrayObject not acting as object
Submitted: 2006-08-09 09:32 UTC Modified: 2006-08-13 09:46 UTC
From: ante at novisplet dot com Assigned: helly (profile)
Status: Not a bug Package: SPL related
PHP Version: 5.1.* OS: *
Private report: No CVE-ID: None
Welcome back! If you're the original bug submitter, here's where you can edit the bug or add additional notes.
If this is not your bug, you can add a comment by following this link.
If this is your bug, but you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: ante at novisplet dot com
New email:
PHP Version: OS:

 

 [2006-08-09 09:32 UTC] 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...

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2006-08-09 09:46 UTC] 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 10:23 UTC] 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-13 09:46 UTC] helly@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

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
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Thu Apr 18 18:01:28 2024 UTC