php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #52251 in __set() $obj->{222} = $foo; 222 is casted to string
Submitted: 2010-07-05 14:35 UTC Modified: 2010-08-01 23:09 UTC
From: giorgio dot liscio at email dot it Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 5.3.2 OS: all?
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 you forgot your password, you can retrieve your password here.
Password:
Status:
Package:
Bug Type:
Summary:
From: giorgio dot liscio at email dot it
New email:
PHP Version: OS:

 

 [2010-07-05 14:35 UTC] giorgio dot liscio at email dot it
Description:
------------
hi

class MyArray
{
   public function __set($i,$v){var_dump($i);}
   public function __get($i){var_dump($i);}
}

$obj = new MyArray();
$obj->{222} = "foo";
echo $obj->{222};

i think the index of the __set __get magic methods should be as user passed
with the characteristics of a "php's valid array key"








but in plus...
would be nice if the key accepts all types too

$hashtable->{new User(2222)} = new SomeUserData(2222);

here User is casted to "string" and it is good for hash tables, but inside the __set method is impossible to access the original "new User" instance


Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-07-05 21:00 UTC] giorgio dot liscio at email dot it
arrayaccess interface has a different behavior and allows to pass instances as keys

anyway there's another issue about "classic" arrays

$myarray[new MyClass()] = "foo";

when used in this context, the instance should call __toString to return the key as string... or toScalar in the next php releases
 [2010-07-30 08:45 UTC] bastard dot internets at gmail dot com
giorgio dot liscio at email dot it - your last example...

$myarray[new MyClass()] = "foo";

... is solvable by doing this, provided a __toString method is declared...

// for associative array, you have to manually build the string

$array["'".new MyClass('key1')."'"] = "foo";
$array["'".($obj = new MyOtherClass("test", "something"))."'"] = $obj->prop2;


// for numbered array, you first have to have __toString cast the numeric property to (string) before returning

$array[(int) (string) new MyThirdClass(222)] = "foo";



A __toScalar would be really nice.  But a problem still is the processor wouldn't know if you meant for the array key to be a string, an int, a constant, or what.
 [2010-07-30 08:52 UTC] bastard dot internets at gmail dot com
Woops!  I was too quick.  For associate arrays, instead of building the complete string manually, you just cast the object to string...

$array[(string) new Obj] = 1;
 [2010-08-01 23:09 UTC] felipe@php.net
-Status: Open +Status: Bogus
 [2010-08-01 23:09 UTC] felipe@php.net
Not a bug.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Dec 06 04:00:01 2025 UTC