Looking for more information on how to do PHP the right way? Check out PHP: The Right Way

Derick Rethans' Blog:
Overloaded properties (__get)
Aug 19, 2006 @ 20:29:56

Derick Rethans talks about something he noticed when working with backwards compatibility to PHP 5.1 for the eZ components project - the first of which is that __get doesn't behave itself in some situations.

The first issue is an extra notice in some cases. In our (ezcMailTools) class we implement a method that allows you to "reply" to a parsed e-mail message. you can see we loop over one of the seemingly public variables of the $mail class. However, the ezcMail class does not have this as a public member variable, but instead uses overload.

This all works 'fine' with PHP 5.1, however with PHP 5.2 the following notice was generated for this code:

Notice: Indirect modification of overloaded property ezcMail::$to has no effect in ../Mail/src/tools.php on line 364

The reason for this is that __get() only returns variables in read mode, while foreach() wants a variable in read/write mode as it tries to modify the internal array pointer. As it can't do this PHP 5.2 will now throw a warning on this.

The mentioned code examples are included and he includes the work-around that he found to help keep the issue from popping up again.

tagged: get ez components overload property get ez components overload property

Link:


Trending Topics: