Richard Quadling wrote:
> I'd really like this feature to be part of PHP.
>
> I don't particularly like the use of what looks like a closure for the
> set/get.
>
> I used to code in Delphi and I always like the way in which their
> properties were defined.
>
> Essentially, the setter and getter are normal methods which are cherry
> picked for a property [1].
>
> <?php
> class TimePeriod
> {
> protected $seconds;
>
> public property Hours read getHours write setHours;
>
> protected function getHours()
> {
> return $this->seconds / 3600;
> }
>
> protected function setHours()
> {
> $this->seconds = $value * 3600;
> }
>
> // This property is read-only
> public property Minutes read getMinutes;
>
> protected function getMinutes()
> {
> return $this->seconds / 60;
> }
>
> public property Milliseconds read getMilliseconds write setMilliseconds;
>
> public function getMilliseconds()
> {
> // This method is public
> return $this->seconds * 60;
> }
>
> protected function setMilliseconds()
> {
> // This method is protected
> $this->seconds = $value * 3600;
> }
> }
>
> For me, the advantage here is that I can independently the methods
> from the property. If I want to force a subclass to implement a
> setter/getter, then I can abstract the function in the base class.
> Sure, some may say that I should be using an interface. I disagree as
> I probably don't want the methods to be public. Protected or even
> private and/or final.
>
> The classic example is one of shapes. Every shape has a public $area
> property, but the value would be provided by an abstract protected
> TShape::getArea(); method. I can also finalise them, so, for example,
> a triangle shape could have a final protected getArea() method and all
> sub classes of triangles (scalene, isosceles, equilateral) would not
> implement their own getArea() method.
>
> The downside is certainly that the code is more verbose than I would
> guess many people would like.
>
> Regards,
>
> Richard.
>
> [1] http://www.delphibasics.co.uk/RTL.asp?Name=Property
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.
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php