Overloaded properties (__get)
While checking whether the eZ components would run with the latest PHP 5.2 release candidate we noticed that there are some things that are not backwards compatible with PHP 5.1.
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. In this method we have the folowing code:
static function replyToMail( ezcMail $mail )
{
// ...
foreach ( $mail->to as $address )
{
// ...
}
// ...
}
As 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 as you can see in this code snippet:
public function __get( $name )
{
switch ( $name )
{
case 'to':
return $this->properties[$name];
}
}
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.
There is a workaround however in the form of casting it to an array. After our changes the code now looks like:
public function __get( $name )
{
switch ( $name )
{
case 'to':
return (array) $this->properties[$name];
}
}
The second issue is related to this. In this case we did not see an extra notice but instead a fatal error:
Fatal error: Cannot assign by reference to overloaded object in .../trunk/Url/src/url.php on line 242
The code around this line is:
if ( array_key_exists( $index, $this->path ) )
{
$this->path[$name] =& $this->path[$index];
}
In this case $this->path is also an overloaded property returning an array. Because __get() returns a read-only variable assigning a reference can not work as that requires a variable to be in read/write mode. Most likely the code didn't work properly in PHP 5.1 either but luckily this is still in an unreleased component. However, I am not aware of a work-around here.
Comments
Why doesn't PHP just return in read/write mode? :)
-
Davey
How about:
foreach($mail->getAddresses() as $whatever) { ... }
Using __get() or other overloading functions is not good code style. In this case you dynamically change your interfaces to a class component which makes the good harder to understand and to maintenance. Use getter-methods for object properties and real properties instead and maybe an interface to hide a specific implementation. Overloading properties of a class has only very few cases where it could make sense to use it. This is not one of them. It is nice, that the PHP developers take care of the availbility of object orientation, but they break the consistent structure it aims with such silly methods of __get() or __sleep(). Other, pure object oriented language don't have these features either and although they use patterns to achieve a good and clean solution. Please make well object oriented style, or PHP will be ill-reputed as a kiddie and unprofessional language forever.
PHP doesn't return __get() magic properties in read/write mode, because it thinks that it can't write to the property. It could probably be avoided if another variable was used instead.
$to = $mail->to;
or
$to = (array) $mail->to;
foreach($to as $address) { }
But I haven't tested this. I'm going to have to change some of my own code also.
However to PHP, any property from __get() may not have the same property in __set() and PHP may not be intelligent to know whether or not both exists.
It is better this way and very annoying.
Is it the same problem as in http://bugs.php.net/bug.php?id=38146 ? Because they said it was fixed...
@FlorentG: The fatal error was changed to a notice in order to "fix" it.
Just wonder why you don't implement a __set() method as a workaround for the second issue you met.
Nice docs, what did you use for those?
We're using php documentor with our own templates and a small patch to facilitate property documentation.
function &__get($name) {...}
This returns a reference instead of a copy, but still has a few quirks.
The only time I have had an issue with the above is when I'm doing a $this->var1 =& $this->var2. (The double reference screws everything up.)
Life Line
What new fresh hell is this?
"Please click here and tick the box if you DO NOT want to be opted in."
And when you click on the non-visible link:
"[ ] I DO NOT want to be opted in."
@jamesholden Have you ever seen the Expanse? One of the main characters shares your name!
I walked 4.4km in 51m58s
I walked 8.2km in 1h40m07s
I walked 2.4km in 38m25s
@Edent With your ActivityPub implementation, have you figured out how to allow quote posts for your bot posts yet?
π· Brown Cap in the Grass
π© Herikhuizerweg, Rheden, Nederland
I walked 0.9km in 11m17s
I walked 2.8km in 25m32s
I walked 4.6km in 1h8m02s
π· Stalkers Lane
π© Graywood Lane, Wealden, United Kingdom
I hiked 23.0km in 4h10m15s
I walked 3.9km in 39m07s
After my PHP 8.5 in Leeds last night, it's now time to head to Rotterdam to give the same talk there tonight!
It's cold out here, but atleast the snow is now gone.
I walked 1.7km in 13m18s
π· Avenue Gardens
π© Princess Road, London Borough of Brent, United Kingdom
π· Leafy Entrance
π© East Heath Road, London Borough of Camden, United Kingdom
π· From Green to Yellow.
π© St John's Wood Road, City of Westminster, United Kingdom
I walked 9.1km in 1h56m24s







Shortlink
This article has a short URL available: https://drck.me/opg-4w0