Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:56806 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 8470 invoked from network); 6 Dec 2011 14:26:17 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 6 Dec 2011 14:26:17 -0000 Authentication-Results: pb1.pair.com header.from=will.fitch@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=will.fitch@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.49 as permitted sender) X-PHP-List-Original-Sender: will.fitch@gmail.com X-Host-Fingerprint: 209.85.216.49 mail-qw0-f49.google.com Received: from [209.85.216.49] ([209.85.216.49:64494] helo=mail-qw0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id F0/52-30975-8062EDE4 for ; Tue, 06 Dec 2011 09:26:16 -0500 Received: by qadc16 with SMTP id c16so1804554qad.8 for ; Tue, 06 Dec 2011 06:26:14 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=subject:mime-version:content-type:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to:x-mailer; bh=6DfbYC1h0YpSmjs44OyBFgLP3lxX7iYAZYjJKoAgyXo=; b=c02l1OAQVRNNVH8jCR1BYPKEu0LrN4KEhYWJucRQx4D++HvjItkUx1pTqRXN5iqkWR jP7EA+RyEd5ViBfPtH7fAZSdxqpL+jvQngVXSG45ja1wBfkCd6eK6xa5XtIjuc2LriZX zUDlypBiEVfR75ciZlo/54T5O9NvJyW8jobSE= Received: by 10.224.116.144 with SMTP id m16mr12387548qaq.19.1323181574015; Tue, 06 Dec 2011 06:26:14 -0800 (PST) Received: from [192.168.1.68] ([68.64.144.221]) by mx.google.com with ESMTPS id k1sm32574793qap.10.2011.12.06.06.26.10 (version=TLSv1/SSLv3 cipher=OTHER); Tue, 06 Dec 2011 06:26:12 -0800 (PST) Mime-Version: 1.0 (Apple Message framework v1251.1) Content-Type: text/plain; charset=iso-8859-1 In-Reply-To: Date: Tue, 6 Dec 2011 09:26:11 -0500 Cc: internals@lists.php.net Content-Transfer-Encoding: quoted-printable Message-ID: <34B6F836-CC50-4470-AD5F-C6F8C471FC65@gmail.com> References: <9570D903A3BECE4092E924C2985CE4853994C39F@MBX202.domain.local> To: Rasmus Schultz X-Mailer: Apple Mail (2.1251.1) Subject: Re: [PHP-DEV] Patch: getters/setters syntax Implementation From: will.fitch@gmail.com (Will Fitch) On Dec 6, 2011, at 8:58 AM, Rasmus Schultz wrote: > On Tue, Dec 6, 2011 at 3:45 AM, Christian Kaps = wrote: >=20 >> Hi, >>=20 >> I also find this syntax confusing and I think it has a huge WTF = factor. >>=20 >> 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 >>=20 >> class Foo { >>=20 >> /** >> * >> */ >> private $_bar; >>=20 >> /** >> * >> */ >> public $bar{ >>=20 >> /** >> * >> */ >> set { >> if ($bar) { >> $this->_bar =3D $bar * 12; >> } else { >> $this->_bar =3D 0 >> } >> } >>=20 >> /** >> * >> */ >> private set { >> if ($this->_bar =3D=3D=3D null) { >> return 0; >> } >>=20 >> return $this->_bar; >> } >> } >>=20 >> /** >> * >> */ >> public function baz() { >>=20 >> } >> } >>=20 >> - What about type hints? >>=20 >> 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 >>=20 >> 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. >>=20 >> What do you think? >>=20 >> Christian >>=20 >=20 > 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. >=20 > It would be nice to also have support for automatic backing fields in > addition though - so something simple like this: >=20 > class BlogPost > { > private $_author; >=20 > public get author() > { > return $this->_author; > } >=20 > public set author(Person $value) > { > $this->_author =3D $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. >=20 > Could be written like this: >=20 > class BlogPost > { > public Person $author; > } >=20 > 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. >=20 > (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...)