Re: C# properties vs. accessors RFC
| From: | Ralph Schindler | Date: | Wed, 23 Jan 2013 20:00:22 +0000 |
| Subject: | Re: C# properties vs. accessors RFC | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-65121@lists.php.net to get a copy of this message | ||
Hi Steve,
2. C# has no issetter/unsetter. IMO customizing these functions is completely unneeded for the vast majority of use cases and could be replaced by simpler logic: isset($this->prop) calls the getter and compares to NULL; unset($this->prop) passes NULL to the setter.I tend to agree here- I don't see a ton of value in isset/unset accessor functions.
3. C# has no auto-implementations. IMO auto implementations have some value removing boilerplate, but we should not make them violate #1. We could have the property syntax specify what field to use for underlying storage, or simply conclude that the boilerplate is worth the clarity.Not completely true: http://msdn.microsoft.com/en-us/library/bb384054.aspx
I think the path forward is to determine how we can serve the same goals as this RFC, but retain the conceptual simplicity of the C# implementation and maybe syntax that strays less from current PHP. I have some ideas that I could start forging into an RFC. Consider this: class Foo { private $_bar; public function get bar { return $this->_bar; } public function set bar { $this->_bar = $value; } }This is too similar to what we have today: class Foo { private $_bar; public function getBar() { return $this->_bar; } public function setBar($value) { $this->_bar = $value; } }
Advantages I see over the RFC: * It's clearer that the accessors map to regular methods, and you know how to control visibility of methods and how they're inherited. * It's clearer $bar doesn't exist as a property, and it will never show up in state inspection. * You know $_bar is a plain PHP property, and you can initialize it directly in the constructor; we don't need an "initter". * There are no guards/proxies/shadow property to think aboutPersonally, I don't see why 'default' can't be used: class Foo { public $bar { get; set; default 5; } } This solves the var_dump() problem, and if people want dynamic get returning something other than the property/field value, so be it. C# does indeed have an internal field per property though, even if it is an anonymous backing field. -ralph