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
I've finished reading This Way Up. It's about maps, that went wrong.
It's a good read, but htyerr were several chapters that were written in a novel way (as a video transcript, a series of letters), and I found distracting from the a tail content. It'll have worked better in a produced video.
No mention of @openstreetmap though :-(
Updated a bench
Created a tree; Updated 3 humps and a waste_basket
The Early Cormorant Catches the Eel
Sorry, not the best photo! But I caught this Cormorant catching this large eel when looking for Bank Swallows, right next to Eel Pie Island in the Thames.
#Birds #BirdPhotography #BirdsOfMastodon #Photography #London
Updated an estate_agent office
I went to my nieces' birthday party yesterday.
The theme was pink, and that included all the food, mostly died with beet root.
Shock and horror this morning when doing number two. Not only was my turd dark red, it was also glittering at me. Apparently the carrot cake had edible glitter...
So now I know what's worse than glitter.
😂 ✨ 💩 🟣Long-Tailed Tit on a Branch with Lichen
I've been spending some time in random London local nature reserves.
Sitting and listening, and in fifteen minutes you spot countless species.
This one was in Ham Lands Local Nature Reserve near Teddington.
#london #BirdPhotogaphy #BirdsOfMastodon #Birds #LichenSubscribe
A Colourful Mandarin
In The Long Water in Kensington Palace Gardens, London.
Created 7 benches
Created 2 benches
Created a bench
I walked 7.3km in 2h28m39s
Added a note about a duplicate Papersmiths
I walked 4.1km in 49m02s
Fixed website
fix typo
Updated a bench
I walked 1.6km in 20m26s
I walked 1.1km in 11m49s
The Yellow Eye
A blue heron's head, with its very yellow stare-y eye.
#BirdPhotography #Photography #BirdsOfFediverse #BirdsOfMastodon #London
My little Lego box is telling me it really is quite warm outside.
Created a bicycle_parking and a crossing
I walked 3.3km in 41m56s








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