php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #33015 ArrayObject: object- and array-access not interchangeable
Submitted: 2005-05-12 12:32 UTC Modified: 2005-06-20 22:54 UTC
From: pecoes at xs4all dot nl Assigned: helly (profile)
Status: Closed Package: SPL related
PHP Version: 5.0.* OS: *
Private report: No CVE-ID: None
 [2005-05-12 12:32 UTC] pecoes at xs4all dot nl
Description:
------------
It's pretty counter-intuitive, that you can neither access an ArrayObject's properties with the array-syntax, nor its array-values with the object-syntax. I think these two should be interchangeable. Preferably like in ECMAScript...

Reproduce code:
---------------
$a = new ArrayObject();

$a->one   = 1;
$a['two'] = 2;

echo "\$a->one: $a->one \$a->two: $a->two \n";
echo "\$a[one]: $a[one] \$a[two]: $a[two] \n";

Expected result:
----------------
$a->one: 1 $a->two: 2 
$a[one]: 1 $a[two]: 2 

Actual result:
--------------
$a->one: 1 $a->two:
$a[one]:  $a[two]: 2 

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2005-05-12 23:15 UTC] helly@php.net
I could equip you with a special factory method or a derived class that does this. But the way it works it is not possible to achieve what you want with ArrayObject and its current constructor.
 [2005-05-17 13:42 UTC] pecoes at xs4all dot nl
Thanks for the offer, but I'm not in need of a work-around. IMHO it would be great to have a "simple" solution - "simple" from a user's perspective :)

You've created some pretty fascinating stuff with the SPL and from the looks of it, you're doing this all alone right now, so I really don't want to rush you... Just tell me how big the chances are in the long run!
 [2005-05-17 15:37 UTC] helly@php.net
Yep it's me alone. I'll do something as soon as i find ime then. How about some exchangeArray() method that drops the current array and replaces it with the given one. So the following would do what you want:

class ArrayObjectEx extends ArrayObject
{
  function __construct()
  {
    $this->exchangeArray($this);
  }
}

$obj = new ArrayObjectEx;

 [2005-05-17 15:54 UTC] pecoes at xs4all dot nl
What would "exchangeArray" do?
 [2005-05-17 15:58 UTC] pecoes at xs4all dot nl
The problem is not in the constructor, is it?  __get should return array-values as well and every offsetGet should return property-values as well.
 [2005-05-17 23:04 UTC] pecoes at xs4all dot nl
Turns out the work-around is not terribly complicated:

class ArrayObject2 extends ArrayObject
{     
    
    function __set($prop, $val)
    {
        parent::offsetSet($prop, $val);
    }
    
    function __get($prop)
    {
        return parent::offsetGet($prop);
    }
}

Could you turn that into the default behaviour?
 [2005-06-20 22:54 UTC] helly@php.net
This bug has been fixed in CVS.

Snapshots of the sources are packaged every three hours; this change
will be in the next snapshot. You can grab the snapshot at
http://snaps.php.net/.
 
Thank you for the report, and for helping us make PHP better.

Implemented through setFlags() / getFlags(). Note that you can set control flags in the constructor already.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sat Dec 21 14:01:32 2024 UTC