3 ms·
I'd tried to put together an RFC years ago to introduce groovy-style accessors in PHP. $this->foo would look for a getFoo() method, and execute if it existed,
by jacobyoder 2y ago
I'd tried to put together an RFC years ago to introduce groovy-style accessors in PHP.
$this->foo
would look for a getFoo() method, and execute if it existed, or not if not. Felt like that was easier to reason about, fwiw, but I couldn't get it off the ground. Even then, there were multiple C#-style get/set proposals floating around, so this style seems to be the one more people like. Not a fan of the style, personally, and probably won't use these much directly any time soon. If it helps people maintaining libraries that I use to deliver code that is cleaner and more productive to them... I'm OK with that.
- wvenable 2y agoI'm not a fan of that kind of magic in my languages but such a thing was already easily doable in PHP. You could just have a base class that implements __get and __set so that $this->foo automatically calls $this->getFoo().
- mgkimsal 2y agocan't do that if you declare the properties on the class. __get only works for undefined properties.
- wvenable 2y agoWell don't do that then. :)
- munk-a 2y agoOr, alternatively, use `__call`
- 9dev 2y agoDoctor, doctor, it always hurts when I press here…
- hks0 2y agoThat advice doesn't always work in real life; otherwise for every compiler or linter check in any language, we could drop all those checks and tell the programmers not to do that. E.g. if a base class declares a variable it can potentially break its children. Whose at fault here? I agree with your original comment though. And if the bypass of exisitng fields is badly wanted, somehow marking __get to disregard them makes more sense to me.