On 27 October 2014 20:27, Andrea Faulds <a...@ajf.me> wrote: > > > On 26 Oct 2014, at 19:16, Rowan Collins <rowan.coll...@gmail.com> wrote: > > > > I just had a thought on both the naming and future-proofing concerns of > this proposal: what about pre-emptively reserving the skeleton of the > syntax needed for accessors, without actually implementing them? > > I’ve been thinking about this. While `public readonly $foo;` does work, it > is confusing (only readonly to public) and limiting (no public/private > option). Plus, while it could work with getters/setters, it’s hardly ideal. > > On the other hand, while having C#-style syntax here gives us a full > feature set, I don’t like that it inverts the order of a declaration. To > show you what I mean, here’s a normal property: > > public $foobar; > > Now, here’s a property with C#-style syntax: > > var $foobar { public get; private set; }; > > See how the `public` moved to the right? I don’t like that, I want to > avoid that if possible. I’m all for C#-style syntax with the get and set, > but I don’t like that it moves the visibility specifier. > > Similarly, I don’t like this either: > > public $foobar { get; private set; } > > Now the visibility is on both sides! Yuck. That’s the worst of both worlds. > > What I’d like is probably something like this: > > public/private $foobar; > > 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: > > public/private $foobar { > get { return $this->bar * $this->foo; } > set($value) { $this->bar = $value / $this->foo; } > } > > Sorry, but I don't like this. This means that the visibility modifier is no longer next to the thing that it applies to, IMO this is actually harmful to the readability because the order in which the accessors were defined then becomes significant so you have to either look at the ordering or have the parser enforce a particular order, plus "public/private" looks syntactically weird.
I would suggest something like this: public $foobar { get { return $this->bar * $this->foo; } private set($value) { $this->bar = $value / $this->foo } } ...where only a single visibility modifier is permitted on the left, and this is treated as the *default* visibility for the accessors, which are free to declare another visibility if they want - so the following would be equivalent: public $foobar { public get { return $this->bar * $this->foo; } private set($value) { $this->bar = $value / $this->foo } } Another bonus here is that the var keyword can still be permitted on the left for those who prefer the C# syntax, as a synonym of public (i.e. the current behaviour of var). 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. > > -- > Andrea Faulds > http://ajf.me/ > > > > > > -- > PHP Internals - PHP Runtime Development Mailing List > To unsubscribe, visit: http://www.php.net/unsub.php > >