rfc:propertygetsetsyntax
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision | ||
rfc:propertygetsetsyntax [2010/04/25 01:18] – Added an alternative syntax. lieutenantclone | rfc:propertygetsetsyntax [2011/12/22 15:18] – [Changelog] 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 177: | Line 177: | ||
</ | </ | ||
- | ==== Alternative Syntax ==== | + | Note that " |
+ | |||
+ | **__Alternative Syntax__** | ||
The syntax above attempts to match the syntax of C# properties, as well as PHP method declarations as much as possible. | The syntax above attempts to match the syntax of C# properties, as well as PHP method declarations as much as possible. | ||
Line 192: | Line 194: | ||
get { return $this-> | get { return $this-> | ||
set { $this-> | set { $this-> | ||
- | } | + | };// 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; | ||
+ | } | ||
</ | </ | ||
Line 214: | Line 274: | ||
public property Minutes | public property Minutes | ||
{ | { | ||
- | set { $this->minutes | + | set { $this->seconds |
} | } | ||
}; | }; | ||
Line 379: | Line 439: | ||
} | } | ||
}; | }; | ||
- | </ | ||
- | <code php> | ||
class HalfTimePeriod extends TimePeriod | class HalfTimePeriod extends TimePeriod | ||
{ | { | ||
Line 390: | 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 416: | Line 504: | ||
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 429: | Line 521: | ||
* [[http:// | * [[http:// | ||
* [[http:// | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
===== Changelog ===== | ===== Changelog ===== | ||
Line 441: | Line 537: | ||
- 2010-04-24 Dennis Robinson: Added further clarification to the "What about the readonly keyword?" | - 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 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 " | ||
+ | - 2011-12-22 Dennis Robinson: Added " |
rfc/propertygetsetsyntax.txt · Last modified: 2017/09/22 13:28 by 127.0.0.1