rfc:propertygetsetsyntax
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionNext revisionBoth sides next revision | ||
rfc:propertygetsetsyntax [2010/04/25 00:31] – Updated some variable names to match the PHP manual for __get and __set. lieutenantclone | rfc:propertygetsetsyntax [2011/12/22 15:15] – Added "Implementation" section with a link to Clint's article lieutenantclone | ||
---|---|---|---|
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 < | * Author: Dennis Robinson < | ||
Line 43: | Line 43: | ||
} | } | ||
}; | }; | ||
+ | </ | ||
+ | <code php> | ||
$sheet = new TimeSheet(); | $sheet = new TimeSheet(); | ||
$sheet-> | $sheet-> | ||
Line 49: | Line 51: | ||
</ | </ | ||
- | In the above example, the TimeSheet class can store a set of time periods. | + | In the above example, the TimeSheet class can store a set of time periods. |
What if we want totally different code to apply to each property? | What if we want totally different code to apply to each property? | ||
Line 97: | Line 99: | ||
} | } | ||
}; | }; | ||
+ | </ | ||
+ | <code php> | ||
$time = new TimePeriod(); | $time = new TimePeriod(); | ||
$time-> | $time-> | ||
Line 107: | Line 111: | ||
</ | </ | ||
- | In the above example, the code of the %%__%%get() and %%__%%set() methods quickly becomes very large, complicated, | + | In the above example, the code in the %%__%%get() and %%__%%set() methods quickly becomes very large, complicated, |
**__Broken Inheritance__** | **__Broken Inheritance__** | ||
- | Now what happens if a second class extends this class. | + | Now what happens if a second class extends this class. |
===== Properties in C# ===== | ===== Properties in C# ===== | ||
Line 117: | Line 121: | ||
==== Why do we care about C#? ==== | ==== Why do we care about C#? ==== | ||
- | The current implementations of getters and setters, or properties in various programming languages | + | The current implementations of getters and setters, or properties, in various programming languages almost all have a different syntax. |
==== C# Syntax ==== | ==== C# Syntax ==== | ||
Line 157: | Line 161: | ||
private $seconds; | private $seconds; | ||
+ | // Properties are implemented using the " | ||
public property Hours | public property Hours | ||
{ | { | ||
Line 172: | Line 177: | ||
</ | </ | ||
- | ==== Read-Only And Write-Only Properties ==== | + | Note that " |
- | A property can be read-write, read-only or write-only. | + | **__Alternative Syntax__** |
+ | |||
+ | The syntax above attempts to match the syntax of C# properties, as well as PHP method declarations as much as possible. | ||
<code php> | <code php> | ||
Line 181: | Line 188: | ||
private $seconds; | private $seconds; | ||
- | | + | |
+ | // This creates yet another | ||
+ | public $Hours | ||
{ | { | ||
- | set { $this->minutes | + | |
- | } | + | |
+ | };// Note the semi-colon here | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | In this syntax, a semi-colon exists at the end of the property definition. | ||
+ | |||
+ | **__Alternative Syntax Suggested By jbondc__** | ||
+ | |||
+ | <code php> | ||
+ | property Hours { | ||
+ | get { return $this-> | ||
+ | set { $this-> | ||
+ | } | ||
+ | |||
+ | class TimePeriod | ||
+ | { | ||
+ | private $seconds; | ||
+ | |||
+ | public [Hours] $hours; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | This syntax would favor re-use similar to traits by injecting the set/get code. | ||
+ | |||
+ | The implementation would add the keywords ' | ||
+ | |||
+ | 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.", | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | Another approach, to reserve the ' | ||
+ | |||
+ | <code php> | ||
+ | // property as some kind of trait | ||
+ | class TimePeriod | ||
+ | { | ||
+ | private $seconds; | ||
+ | |||
+ | public {use Hours;} $hours; | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | ==== Read-Only And Write-Only Properties ==== | ||
+ | |||
+ | A property can be read-write, read-only or write-only. | ||
+ | |||
+ | <code php> | ||
+ | class TimePeriod | ||
+ | { | ||
+ | private $seconds; | ||
+ | // This property has no " | ||
public property Hours | public property Hours | ||
{ | { | ||
get { return $this-> | get { return $this-> | ||
+ | } | ||
+ | |||
+ | // This property has no " | ||
+ | public property Minutes | ||
+ | { | ||
+ | set { $this-> | ||
} | } | ||
}; | }; | ||
Line 203: | Line 289: | ||
**__What about the readonly keyword? | **__What about the readonly keyword? | ||
- | There has been talk about adding a " | + | There has been talk about adding a " |
+ | |||
+ | The following is an example of using the " | ||
+ | <code php> | ||
+ | class TimePeriod | ||
+ | { | ||
+ | private $seconds; | ||
+ | |||
+ | public readonly property Hours | ||
+ | { | ||
+ | get { return $this-> | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | class HalfTimePeriod extends TimePeriod | ||
+ | { | ||
+ | public property Hours | ||
+ | { | ||
+ | // The get method is being overloaded | ||
+ | set { return ($this-> | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
Additionally, | Additionally, | ||
Line 234: | Line 342: | ||
Interfaces may define property declarations, | Interfaces may define property declarations, | ||
- | When a class implements an interface that has a read-only property, it can add in a set method to turn the property into a read-write property. | + | When a class implements an interface that defines |
<code php> | <code php> | ||
Line 259: | Line 367: | ||
==== Overloading Properties ==== | ==== Overloading Properties ==== | ||
- | Properties can be overloaded in extending classes. | + | Properties can be overloaded in extending classes. |
<code php> | <code php> | ||
Line 299: | Line 407: | ||
public property Minutes | public property Minutes | ||
{ | { | ||
- | // A set method is adding, turning this property into a read-write property instead of read-only | + | // A set method is added, turning this property into a read-write property instead of read-only |
set { $this-> | set { $this-> | ||
} | } | ||
Line 305: | Line 413: | ||
public property Milliseconds | public property Milliseconds | ||
{ | { | ||
- | // A property method can be made more visible | + | // A property method can have its visibility increased |
- | // This method is now public instead of private | + | // This method is now public instead of protected |
public set | public set | ||
{ | { | ||
- | // You can access a base class property explicitly, just like accessing a base class member or method | + | // You can access a base class property explicitly, just like accessing a base class member or method |
TimePeriod:: | TimePeriod:: | ||
} | } | ||
Line 318: | Line 426: | ||
==== Final Properties ==== | ==== Final Properties ==== | ||
- | Properties declared final are not able to be overloaded in a child class, just like final methods. | + | Properties declared final are not allowed |
<code php> | <code php> | ||
Line 331: | Line 439: | ||
} | } | ||
}; | }; | ||
- | </ | ||
- | <code php> | ||
class HalfTimePeriod extends TimePeriod | class HalfTimePeriod extends TimePeriod | ||
{ | { | ||
Line 342: | Line 448: | ||
{ | { | ||
get { return ($this-> | get { return ($this-> | ||
+ | } | ||
+ | }; | ||
+ | </ | ||
+ | |||
+ | **__Final property methods__** | ||
+ | |||
+ | The get or set method of a property can be declared " | ||
+ | |||
+ | <code php> | ||
+ | class TimePeriod | ||
+ | { | ||
+ | private $seconds; | ||
+ | |||
+ | // Notice there is no " | ||
+ | public property Hours | ||
+ | { | ||
+ | final get { return $this-> | ||
+ | set { $this-> | ||
+ | } | ||
+ | }; | ||
+ | |||
+ | class HalfTimePeriod extends TimePeriod | ||
+ | { | ||
+ | private $seconds; | ||
+ | |||
+ | public property Hours | ||
+ | { | ||
+ | get { return ($this-> | ||
+ | // because it was declared final in the base class | ||
+ | set ( $this-> | ||
} | } | ||
}; | }; | ||
Line 364: | Line 500: | ||
<code php> | <code php> | ||
- | // Accessing | + | // Accessing |
TimePeriod:: | TimePeriod:: | ||
echo TimePeriod:: | echo TimePeriod:: | ||
</ | </ | ||
+ | |||
+ | ===== Implementation ===== | ||
+ | |||
+ | An implementation of this proposal is being worked on by Clint Priest <phpdev at zerocue dot com> | ||
===== References ===== | ===== References ===== | ||
Line 381: | Line 521: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
===== Changelog ===== | ===== Changelog ===== | ||
Line 391: | Line 535: | ||
- 2010-04-24 Dennis Robinson: Updated some incorrect code examples in the section "The Problem" | - 2010-04-24 Dennis Robinson: Updated some incorrect code examples in the section "The Problem" | ||
- 2010-04-24 Dennis Robinson: Fleshed out the "The Current Solution" | - 2010-04-24 Dennis Robinson: Fleshed out the "The Current Solution" | ||
+ | - 2010-04-24 Dennis Robinson: Added further clarification to the "What about the readonly keyword?" | ||
+ | - 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 " | ||
+ | - 2010-11-29 jbondc: Added another alternative syntax | ||
+ | - 2010-12-01 Dennis Robinson: Added additional links in the " |
rfc/propertygetsetsyntax.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1