Re: [RFC] Readonly Properties
| From: | Jordi Boggiano | Date: | Tue, 28 Oct 2014 07:17:53 +0000 |
| Subject: | Re: [RFC] Readonly Properties | ||
| References: | 1 2 3 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-78415@lists.php.net to get a copy of this message | ||
On 27/10/2014 20:27, Andrea Faulds wrote:
Tentative syntax. But this way, the visibility stays on the left. I think that’s good for readability. If you omit the second specifier, then the first one applies to getting and setting, as now. If you include it, the first one applies to getting, the second one to setting. It’d also be compatible with properties, too:I like it, except for the fact that if you add a custom getter to a property suddenly it becomes readonly unless you remember to add "; set" to the end of the block, right? How about this instead for readonly:public/private $foobar { get { return $this->bar * $this->foo; } set($value) { $this->bar = $value / $this->foo; } }It doesn’t prevent truly read-only properties, either:public $foobar { get { return $this->bar * $this->foo; } }Does this sound like a good idea? A similar idea came up in the discussions 8 years ago on readonly.
public $foobar {
get { return $this->bar * $this->foo; }; readonly
}
And if the flag isn't there, set is implicitly present.
That'd mean you also can keep "public readonly $foobar;" as a shorthand for readonly properties without custom getter.
Cheers
--
Jordi Boggiano
@seldaek - http://nelm.io/jordi