On 11/29/10 8:30 AM, Ángel González wrote:

What about allowing this syntax to attach the property to a variable?

For instance:

<?php
class TimePeriod
{
     protected $seconds;
     protected $minutes;
     protected $hours;

     public property Seconds read $seconds write setSeconds;
     public property Minutes read $seconds write setMinutes;
     public property Hours read $seconds write setHours;

     public function setSeconds($seconds)
     {
         if ($seconds>= 0&&  $seconds<  60) $this->seconds = $seconds;
     }

     public function setMinutes($minutes)
     {
         if ($minutes>= 0&&  $minutes<  60) $this->minutes = $minutes;
     }

     public function setHours($hours)
     {
         if ($hours>= 0&&  $hours<  24) $this->hours = $hours;
     }

}

We only want to perform checks on write, so instead of writing the trivial
getters, the property is set to the variable itself. Child classes could
reattach it to a function if needing more control.

Another advantage here would presumably be performance. If there's no getter defined then the engine could simply map $foo->bar to the class member directly (which is really fast) and not to a method, so there's no added overhead there. That still leaves the question of what happens with name collisions, though.

... and that makes me think that someone is sure to ask about runtime changes to the property structure sooner or later, as we keep asking about methods (and *sort of* have figured out with binding closures to objects, if that did actually get committed), so I'll go ahead and ask it now. :-)

--Larry Garfield

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to