On Mon Nov 29 09:27 AM, Stas Malyshev wrote:
> Hi!
> 
> > Nice RFC, just an idea for an alternative syntax (added to the RFC 
> > as
> #2):
> >
> > property Hours {
> >     get { return $this->seconds / 3600; }
> >     set { $this->seconds = $value * 3600; } // The variable $value
> holds
> > the incoming value to be "set"
> > }
> >
> > class TimePeriod
> > {
> >      private $seconds;
> >
> >      public [Hours] $hours1;
> >      public {use Hours;} $hours2;
> > }
> 
> If you change "property" to "class" or "trait" and "get" to "__get" 
> you need almost no new syntax :)
> 

Right, it looks the same but the subtle difference is 'property Hours'
wouldn't be registered as a class. It's just container code for get(), set()
methods that would get 'compiled' into opcodes in the class TimePeriod (the
property exists vs. searching for it in runtime). So you can think of it as
a special 'trait' that only applies to properties.

The idea behind this syntax is you can move the 'property' definition out of
the class so that you can test and re-use it somewhere else (like traits).

That might not be problem if you can define properties in traits (needs to
be explained in the RFC):
trait TimeUnits {
    public property Seconds
    {
        get { return $this->seconds; }
        set { $this->seconds = $value; }// The variable $value holds the
incoming value to be "set"
    }
    public property Minutes
    {
        get { return $this->seconds / 60; }
        set { $this->seconds = $value * 60; }
    }
    public property Hours
    {
        get { return $this->seconds / 3600; }
        set { $this->seconds = $value * 3600; }// The variable $value holds
the incoming value to be "set"
    }
}

class MyTime {
  uses TimeUnits;

  protected $_seconds;
}




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

Reply via email to