Re: RFC: C-sharp style property get/set syntax for PHP

From: Date: Mon, 29 Nov 2010 16:10:13 +0000
Subject: Re: RFC: C-sharp style property get/set syntax for PHP
References: 1 2 3  Groups: php.internals 
Request: Send a blank email to internals+get-50697@lists.php.net to get a copy of this message
2010/11/29 Ángel González <keisial@gmail.com>: > Richard Quadling wrote: > setMilliseconds() should have $value as parameter instead of a magic name.. > > 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. Ouch. I messed up on the example code. Completely forget that there was a parameter to process... As for reading $seconds directly ... Well. If you think of the element that follows read as $this->xxxx, then if the parser can handle both ... read $seconds read getSeconds then yes for both. If not, then I'd guess that the getSeconds version should be the one we use as the value may not actually exist without some processing. Richard. -- Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY

« previous php.internals (#50697) next »