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
In times like this, it's actually fairly useful to be able to read a fair amount of Danish.
Merge branch 'v2022'
Go with 2022.16
Merge branch 'v2022'
Merge branch 'v2022'
Go with 2022.15
Do a shallow clone
Merge branch 'v2022'
Update data to 2025c
Merge branch 'v2022'
Update script for current PHP versions
I hold the domains phpvikinger.{org,net,com}, which are expiring in three months.
I am not going to renew them as I have no use for them any more.
If you're interested, let me know? It'll cost you just the normal registration fees.
I walked 4.8km in 44m52s
I walked 2.1km in 20m46s
I walked 5.8km in 1h12m05s
Robin
They usually never sit still long enough close by for a good photo. But this little bird was perched on this branch for several minutes letting lots of people take photos of it. When I started taking photos, it looked directly at me and my camera — it was clearly posing!
I walked 10.5km in 1h48m41s
I hiked 16.2km in 3h23m19s
Updated a restaurant
Updated a restaurant
I walked 8.7km in 1h25m51s
Merged pull request #1056
Add metadata header to CSV file
RE: https://phpc.social/@Tideways/115859809842500892


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