Re: Patch: getters/setters syntax Implementation
| From: | Will Fitch | Date: | Tue, 06 Dec 2011 14:26:11 +0000 |
| Subject: | Re: Patch: getters/setters syntax Implementation | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-56806@lists.php.net to get a copy of this message | ||
On Dec 6, 2011, at 8:58 AM, Rasmus Schultz wrote:
> On Tue, Dec 6, 2011 at 3:45 AM, Christian Kaps <christian.kaps@mohiva.com>wrote:
>
>> Hi,
>>
>> I also find this syntax confusing and I think it has a huge WTF factor.
>>
>> Some thoughts about the syntax:
>> - At the first glance, it isn't clear which visibility the getter or
>> setter has
>> - The extra indentation level makes the code more unreadable
>>
>> class Foo {
>>
>> /**
>> *
>> */
>> private $_bar;
>>
>> /**
>> *
>> */
>> public $bar{
>>
>> /**
>> *
>> */
>> set {
>> if ($bar) {
>> $this->_bar = $bar * 12;
>> } else {
>> $this->_bar = 0
>> }
>> }
>>
>> /**
>> *
>> */
>> private set {
>> if ($this->_bar === null) {
>> return 0;
>> }
>>
>> return $this->_bar;
>> }
>> }
>>
>> /**
>> *
>> */
>> public function baz() {
>>
>> }
>> }
>>
>> - What about type hints?
>>
>> I prefer a more AS3 like getter and setter syntax.
>>
>> http://help.adobe.com/en_US/**ActionScript/3.0_**ProgrammingAS3/**
>> WS5b3ccc516d4fbf351e63e3d118a9**b90204-7f30.html#**
>>
>> WS5b3ccc516d4fbf351e63e3d118a9**b90204-7fcb<http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7f30.html#WS5b3ccc516d4fbf351e63e3d118a9b90204-7fcb>
>>
>> Have you read my previous mail
>> http://news.php.net/php.**internals/56762<http://news.php.net/php.internals/56762>
>> .
>> I think this syntax fits more to PHP because its similar to the already
>> existing(magic) getter and setter syntax.
>>
>> What do you think?
>>
>> Christian
>>
>
> I agree with all of those points - the extra indentation looks messy, and
> yes, type hints are important. It does fit better with PHP in general.
>
> It would be nice to also have support for automatic backing fields in
> addition though - so something simple like this:
>
> class BlogPost
> {
> private $_author;
>
> public get author()
> {
> return $this->_author;
> }
>
> public set author(Person $value)
> {
> $this->_author = $value;
> }
> }
I don't like this approach. All efforts (which I'm currently part of) to implement type
hinting return values will be compromised. If you want to implement accessors, keep them within a
syntax that makes sense. Personally, I support the C# style as much as possible. Methods are
already overused for purposes they shouldn't be, so if we're attempting to get around
__set/get, let's not replace them with more method implementations.
>
> Could be written like this:
>
> class BlogPost
> {
> public Person $author;
> }
>
> Effectively, this shorthand syntax just gives you type-safe properties -
> but it refactors nicely, since you can replace it with a full
> implementation of a backing field at any point.
>
> (on second thought, I don't like the idea I suggested before - adding a
> magical $value in accessors, similar to $this - it's confusing and it's
> going to look like an undeclared local variable...)