Yet another reason SuperGlobal sucks...
On Mon, Feb 2, 2009 at 12:56 PM, Matt Read <[email protected]> wrote:
>
> Just a note about using overloaded properties in PHP 5.2.0, since GPC
> are now objects (SuperGlobal)
>
> In PHP 5.2.0 (fixed in 5.2.1), all overloaded properties (using magic
> method __get()) are returned in "read-only" context. If you try to
> write to an overloaded array, or directly loop with foreach, PHP 5.2.0
> throws a fatal error. For example:
>
> $my_obj // a stdObj with overloaded property "some_array" (which is an
> array)
>
> // -- these work
> $my_obj->some_array = array('foo'=>'bar'); // call __set() and sets the
> array
> echo $my_obj->some_array['foo']; // calls __get(), returns the array
> in read context, and echos the 'foo' element
>
>
> // -- these do not work
> $my_obj->some_array['far'] = 'boo'; // calls __get(), returns the
> array in read context, and attempts to write to read-only. Causes
> fatal error.
> // solution
> $a = $my_obj->some_array; //calls __get(), and assign to new var $a
> $a['far'] = 'boo'; // sets the 'far' element in
> $a
> $a = $my_obj->some_array = $a; // calls __set(), setting the some_array to
> $a
>
>
> foreach( $my_obj->some_array as $foo) { // calls __get(), returns the
> array in read mode, and attempts to loop.
> // Causes
> fatal error, because foreach requires var be passed by reference.
> //solution
> $a = $my_obj->some_array; //calls __get(), and assign to new var $a
> foreach( $a as $foo) { // passes new var $a by reference
>
>
> Now, there is one catch, and that is an ArrayObject/ArrayIterator
> object. They return properties in read/write context. (This applies to
> our new SuperGlobal objects)
>
> $my_arrayObject // an ArrayObject with property "some_array" (which is an
> array)
> $my_arrayObject['some_array']; // return the array $some_array in
> read/write context
> $my_arrayObject['some_array']['far'] = 'boo'; // this works as the
> array is in read/write context
>
> foreach($my_arrayObject['some_array'] as $foo) { // Causes fatal error
> as foreach requires to be passed by reference
> //solution
> $a = $my_arrayObject['some_array']; // assigns to new var $a
> foreach( $a as $foo) { // passes new var $a by reference
>
>
> Just something to keep in mind when using our new SuperGlobal ($_POST,
> $_GET, $_COOKIE) objects. Also, this only applies to PHP 5.2.0, which
> is default in the latest stable Debian, etch, so we have to "support"
> this bug.
>
> NOTE: you can also overcome this by setting the __get to return by
> reference (function & __get() {) if, and only if, your parent class
> allows for it.
>
> I hope I made sense through all that :)
>
> --
> Matt Read
> http://mattread.com
>
> >
>
--~--~---------~--~----~------------~-------~--~----~
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at http://groups.google.com/group/habari-dev
-~----------~----~----~----~------~----~------~--~---