|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[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
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Dec 06 04:00:01 2025 UTC |
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.