Re: Improving PHP's Object Egonomics: A broad analysis

From: Date: Wed, 25 Mar 2020 16:20:14 +0000
Subject: Re: Improving PHP's Object Egonomics: A broad analysis
References: 1 2 3 4  Groups: php.internals 
Request: Send a blank email to internals+get-109298@lists.php.net to get a copy of this message
Am 25.03.20 um 15:24 schrieb Larry Garfield: > On Wed, Mar 25, 2020, at 5:57 AM, Nicolas Grekas wrote: >> Máté suggested this syntax and it has my preference over the one you menton >> Larry: doubling the visibility keyword could be enough to express >> read+write access: >> >> public private $property; <= public read access, private write access > > In that syntax, you have to remember which one comes first. There's no indication for the > casual reader why > > public private $property; > > and > > private public $property; > > are different. Plus, just looking at it, "wait, it's public and private? WTF? That > doesn't even make sense." > > It also doesn't extend gracefully to property accessors. Whatever accessors do, if they > ever get resolved, would conflict with that, and thus we'd have that many more weird > combinations of property metadata that are incompatible. > > The whole point of the syntax I proposed for asymmetric visibility is that it's gracefully > extensible, even if a little more verbose. (If we can find a syntax that is less verbose while > still gracefully extensible, I am on board with that.) What about the following syntax: class X { public read private write $property; } That would play nicely with accessors: class Y { public read getProp private write setProp Type $property; // or this way around to make it clear which is the setter // and which is the property type public getProp read private setProp write Type $property; private function getProp() : ?Type {} private function setProp (Type $newValue) {} } It seems to me this would allow a clear syntax where you can selectively add accessor method for reading or writing or both, you can reuse existing setting methods when refactoring. Only the isset/unset accessors are missing. I am not sure if they are necessary: isset could be equivalent to ($obj->getProp() !== null) and unset to setting null, but I have not thought this through yet. Greets Dennis

« previous php.internals (#109298) next »