rfc:propertygetsetsyntax

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
rfc:propertygetsetsyntax [2010/04/25 01:00] – Further clarification and wording fixes. lieutenantclonerfc:propertygetsetsyntax [2017/09/22 13:28] (current) – external edit 127.0.0.1
Line 1: Line 1:
 ====== Request for Comments: Property get/set syntax ====== ====== Request for Comments: Property get/set syntax ======
-  * Version: 0.1+  * Version: 0.3
   * Date: 2009-09-13   * Date: 2009-09-13
   * Author: Dennis Robinson <president at basnetworks dot net>   * Author: Dennis Robinson <president at basnetworks dot net>
Line 161: Line 161:
     private $seconds;     private $seconds;
  
 +    // Properties are implemented using the "property" keyword, just like functions/methods use the "function" keyword
     public property Hours     public property Hours
     {     {
Line 174: Line 175:
 $time->Hours = 12;// Stored as 43200 $time->Hours = 12;// Stored as 43200
 echo $time->Hours;// Outputs 12 echo $time->Hours;// Outputs 12
 +</code>
 +
 +Note that "get" and "set" as seen above would become new keywords.  Alternatively, "get" and "set" could be made to only have meaning if they are in a prototype, similar to the current type hinting syntax that exists in php-trunk.
 +
 +**__Alternative Syntax__**
 +
 +The syntax above attempts to match the syntax of C# properties, as well as PHP method declarations as much as possible.  An alternative syntax could look like the following:
 +
 +<code php>
 +class TimePeriod
 +{
 +    private $seconds;
 +
 +    // Looks less like a function and more like a class member
 +    // This creates yet another property syntax by moving away from the C# syntax too much
 +    public $Hours
 +    {
 +        get { return $this->seconds / 3600; }
 +        set { $this->seconds = $value * 3600; }// The variable $value holds the incoming value to be "set"
 +    };// Note the semi-colon here
 +};
 +</code>
 +
 +In this syntax, a semi-colon exists at the end of the property definition.  This was suggested by Kalle Nielsen, as it would be simpler to implement into the current implementation of class members in the PHP interpreter.
 +
 +**__Alternative Syntax Suggested By jbondc__**
 +
 +<code php>
 +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] $hours;
 +}
 +</code>
 +
 +This syntax would favor re-use similar to traits by injecting the set/get code.
 +
 +The implementation would add the keywords 'property', _PROPERTY_, the '[', ']' tokens and the 'readonly' property as part of Spl.
 +
 +A read-only property could be defined in the following ways:
 +
 +<code php>
 +// Spl property
 +property readonly  {
 +    final set { throw Exception(__PROPERTY__ . " is read-only."); }
 +}
 +
 +// Read-only property #1
 +property MyHours1 extends Hours  {
 +     use readonly;
 +}
 +
 +// Read-only property #2
 +property MyHours2 extends Hours  {
 + set { throw Exception(__PROPERTY__ . " is read-only."); }
 +}
 +
 +// Read-only property #3 (if you don't want an exception)
 +property MyHours3 extends Hours  {
 + set { trigger_error(__PROPERTY__ . " is read-only.", E_USER_ERROR); }
 +}
 +</code>
 +
 +Another approach, to reserve the '[', ']' tokens for annotations or something else could be:
 +
 +<code php>
 +// property as some kind of trait
 +class TimePeriod
 +{
 +    private $seconds;
 +
 +    public {use Hours;} $hours;
 +}
 </code> </code>
  
Line 194: Line 274:
     public property Minutes     public property Minutes
     {     {
-        set { $this->minutes = $value * 60; }+        set { $this->seconds = $value * 60; }
     }     }
 }; };
Line 359: Line 439:
     }     }
 }; };
-</code> 
  
-<code php> 
 class HalfTimePeriod extends TimePeriod class HalfTimePeriod extends TimePeriod
 { {
Line 370: Line 448:
     {     {
         get { return ($this->seconds / 3600) / 2; }         get { return ($this->seconds / 3600) / 2; }
 +    }
 +};
 +</code>
 +
 +**__Final property methods__**
 +
 +The get or set method of a property can be declared "final" independently of each other.  This would allow for one of them to be overloaded, but not the other.
 +
 +<code php>
 +class TimePeriod
 +{
 +    private $seconds;
 +
 +    // Notice there is no "final" keyword on the property declaration
 +    public property Hours
 +    {
 +        final get { return $this->seconds / 3600; }// Only the get method is declared final
 +        set { $this->seconds = $value * 3600; }
 +    }
 +};
 +
 +class HalfTimePeriod extends TimePeriod
 +{
 +    private $seconds;
 +
 +    public property Hours
 +    {
 +        get { return ($this->seconds / 3600) / 2; }// This attempt to overload the get method of the "Hours" will throw an error
 +                                                   // because it was declared final in the base class
 +        set ( $this->seconds = ($value * 3600) * 2; )// This would be OK
     }     }
 }; };
Line 396: Line 504:
 echo TimePeriod::$Hours;// Outputs 12 echo TimePeriod::$Hours;// Outputs 12
 </code> </code>
 +
 +===== Implementation =====
 +
 +An implementation of this proposal is being worked on by Clint Priest <phpdev at zerocue dot com> Information about implementation details can be found here: https://wiki.php.net/rfc/propertygetsetsyntax-as-implemented
  
 ===== References ===== ===== References =====
Line 409: Line 521:
   * [[http://msdn.microsoft.com/en-us/library/x9fsa0sw%28VS.80%29.aspx|Properties (C# Programming Guide)]]   * [[http://msdn.microsoft.com/en-us/library/x9fsa0sw%28VS.80%29.aspx|Properties (C# Programming Guide)]]
   * [[http://msdn.microsoft.com/en-us/library/75e8y5dd%28VS.80%29.aspx|Asymmetric Accessor Accessibility (C# Programming Guide)]]   * [[http://msdn.microsoft.com/en-us/library/75e8y5dd%28VS.80%29.aspx|Asymmetric Accessor Accessibility (C# Programming Guide)]]
 +  * [[http://msdn.microsoft.com/en-us/library/ms229054.aspx|Choosing Between Properties and Methods (MSDN)]]
 +  * [[http://msdn.microsoft.com/en-us/library/ms229006.aspx|Property Design (MSDN)]]
 +  * [[http://c2.com/cgi/wiki?UniformAccessPrinciple|Uniform Access Principle (C2 wiki)]]
 +  * [[http://en.wikipedia.org/wiki/Uniform_access_principle|Uniform Access Principle (Wikipedia)]]
  
 ===== Changelog ===== ===== Changelog =====
Line 420: Line 536:
   - 2010-04-24 Dennis Robinson: Fleshed out the "The Current Solution" section description.   - 2010-04-24 Dennis Robinson: Fleshed out the "The Current Solution" section description.
   - 2010-04-24 Dennis Robinson: Added further clarification to the "What about the readonly keyword?" section description, and added a code example.   - 2010-04-24 Dennis Robinson: Added further clarification to the "What about the readonly keyword?" section description, and added a code example.
 +  - 2010-04-24 Dennis Robinson: Added an alternative syntax. 
 +  - 2010-04-24 Dennis Robinson: Added an example of property methods being marked final individually. 
 +  - 2010-11-13 Dennis Robinson: Added a note below the basic syntax about new keywords being created. 
 +  - 2010-11-13 Dennis Robinson: Added a semicolon to the "alternative syntax", as suggested by Kalle Nielsen. 
 +  - 2010-11-29 jbondc: Added another alternative syntax 
 +  - 2010-12-01 Dennis Robinson: Added additional links in the "Further Reading" section 
 +  - 2011-12-22 Dennis Robinson: Added "Implementation" section
rfc/propertygetsetsyntax.1272157221.txt.gz · Last modified: 2017/09/22 13:28 (external edit)