Edit report at http://bugs.php.net/bug.php?id=52251&edit=1

 ID:                 52251
 Comment by:         bastard dot internets at gmail dot com
 Reported by:        giorgio dot liscio at email dot it
 Summary:            in __set() $obj->{222} = $foo; 222 is casted to
                     string
 Status:             Open
 Type:               Bug
 Package:            Scripting Engine problem
 Operating System:   all?
 PHP Version:        5.3.2
 Block user comment: N

 New Comment:

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.


Previous Comments:
------------------------------------------------------------------------
[2010-07-05 21:00:13] 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-05 14:35:24] 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



------------------------------------------------------------------------



-- 
Edit this bug report at http://bugs.php.net/bug.php?id=52251&edit=1

Reply via email to