Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:56802 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 551 invoked from network); 6 Dec 2011 13:58:29 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Dec 2011 13:58:29 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.212.42 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.212.42 mail-vw0-f42.google.com Received: from [209.85.212.42] ([209.85.212.42:64606] helo=mail-vw0-f42.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F4/A0-30975-38F1EDE4 for ; Tue, 06 Dec 2011 08:58:28 -0500 Received: by vbbfd1 with SMTP id fd1so5082360vbb.29 for ; Tue, 06 Dec 2011 05:58:25 -0800 (PST) MIME-Version: 1.0 Received: by 10.182.89.105 with SMTP id bn9mr1944854obb.65.1323179904809; Tue, 06 Dec 2011 05:58:24 -0800 (PST) Received: by 10.182.44.130 with HTTP; Tue, 6 Dec 2011 05:58:24 -0800 (PST) In-Reply-To: References: <9570D903A3BECE4092E924C2985CE4853994C39F@MBX202.domain.local> Date: Tue, 6 Dec 2011 08:58:24 -0500 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=14dae93994af333afc04b36cd4f7 Subject: Re: [PHP-DEV] Re: Patch: getters/setters syntax Implementation From: rasmus@mindplay.dk (Rasmus Schultz) --14dae93994af333afc04b36cd4f7 Content-Type: text/plain; charset=ISO-8859-1 On Tue, Dec 6, 2011 at 3:45 AM, Christian Kaps 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 > > Have you read my previous mail 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; } } 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...) --14dae93994af333afc04b36cd4f7--