Hello,

>> 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;
> }


I do not think that properties should make use of a trait-like syntax, as
that is not what a property is about.  A property is basically a layer of
syntactic sugar over a pair of methods.  The majority of the time when
writing properties, you will not want to re-use them, so I have a hard
time seeing many parallels to traits.

However, it does make sense to be able to define a property as part of a
trait, as again, it is basically just a pair of methods.  When I get some
time, I will try to add a syntax for traits to the RFC.

- Dennis


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

Reply via email to