Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:50734 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 64650 invoked from network); 30 Nov 2010 15:01:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Nov 2010 15:01:45 -0000 Authentication-Results: pb1.pair.com smtp.mail=rquadling@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=rquadling@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.216.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: rquadling@gmail.com X-Host-Fingerprint: 209.85.216.170 mail-qy0-f170.google.com Received: from [209.85.216.170] ([209.85.216.170:38871] helo=mail-qy0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 05/25-35426-4D115FC4 for ; Tue, 30 Nov 2010 10:01:42 -0500 Received: by qyk10 with SMTP id 10so1270510qyk.8 for ; Tue, 30 Nov 2010 07:01:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:reply-to :in-reply-to:references:from:date:message-id:subject:to:cc :content-type:content-transfer-encoding; bh=QGU64vdefjMH/OHVCfxMfGG2/c2YF/5/4Wq2UpjXnhA=; b=bWzw54nKNxy8AJfHxkn7PY5COPbsNCJ/3LfPVB0SUibo0wIdcFt0huVpLetjL8IM3F DQJ4JKpxjU8PcR56tpE7+tA9iODQu4DAmz4hwaSJY81vUmhWV9egUD8hECiv42BsnFdg dwfIOb9H0VTAAJBGt46MGjgyc6WFNzvymPROM= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:reply-to:in-reply-to:references:from:date:message-id :subject:to:cc:content-type:content-transfer-encoding; b=QMWYV3Pb2ro84NcYzwBHgXV33JXW8CaLoGhczeDBFK4rEVbArPXI6kj+JCwmScfsE7 UsvXltucL3X1AcpeI6OlAENIajPecR91m9ZVaWlrHglOofv8OW6yGlEZ9COCXssVZLZX B3NbelV46Cwr4+jgp3ayiaeRVeEg/MxlD0qmk= Received: by 10.229.192.76 with SMTP id dp12mr6288392qcb.63.1291129297425; Tue, 30 Nov 2010 07:01:37 -0800 (PST) MIME-Version: 1.0 Received: by 10.229.100.130 with HTTP; Tue, 30 Nov 2010 07:01:17 -0800 (PST) Reply-To: RQuadling@googlemail.com In-Reply-To: <073c59f255a348e9684fb4875490b23f.squirrel@webmail.basnetworks.net> References: <073c59f255a348e9684fb4875490b23f.squirrel@webmail.basnetworks.net> Date: Tue, 30 Nov 2010 15:01:17 +0000 Message-ID: To: president@basnetworks.net Cc: internals@lists.php.net Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] RFC: C-sharp style property get/set syntax for PHP From: rquadling@gmail.com (Richard Quadling) On 30 November 2010 12:48, wrote: > Hi Richard, > > > >> I'd really like this feature to be part of PHP. >> >> I don't particularly like the use of what looks like a closure for the >> set/get. > > While it looks like a closure, it may not necessarily be one. =C2=A0What = I have > presented in my RFC is a syntax, but I make little assumption about how i= t > would be implemented, as that would be out stepping my expertise. > > In C#, when a property gets compiled it is actually turned into two norma= l > class methods in a special namespace. =C2=A0Looking at a compiled C# libr= ary > with reflector will reveal this underlying implementation. =C2=A0So when = a call > to a property is compiled in C#, it is simple replaced with a call to a > method. =C2=A0The properties themselves are nothing more than syntactic s= ugar. > > Since PHP is interpreted instead of compiled, this may not be an ideal > solution, but I couldn't guess as to what would be a better method. > Preferably something that re-uses the existing class method > interpretation. > > >> I used to code in Delphi and I always like the way in which their >> properties were defined. >> >> Essentially, the setter and getter are normal methods which are cherry >> picked for a property [1]. >> >> > class TimePeriod >> { >> =C2=A0 =C2=A0 protected $seconds; >> >> =C2=A0 =C2=A0 public property Hours read getHours write setHours; >> >> =C2=A0 =C2=A0 protected function getHours() >> =C2=A0 =C2=A0 { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 return $this->seconds / 3600; >> =C2=A0 =C2=A0 } >> >> =C2=A0 =C2=A0 protected function setHours() >> =C2=A0 =C2=A0 { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 $this->seconds =3D $value * 3600; >> =C2=A0 =C2=A0 } >> >> =C2=A0 =C2=A0 // This property is read-only >> =C2=A0 =C2=A0 public property Minutes read getMinutes; >> >> =C2=A0 =C2=A0 protected function getMinutes() >> =C2=A0 =C2=A0 { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 return $this->seconds / 60; >> =C2=A0 =C2=A0 } >> >> =C2=A0 =C2=A0 public property Milliseconds read getMilliseconds write >> setMilliseconds; >> >> =C2=A0 =C2=A0 public function getMilliseconds() >> =C2=A0 =C2=A0 { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 // This method is public >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 return $this->seconds * 60; >> =C2=A0 =C2=A0 } >> >> =C2=A0 =C2=A0 protected function setMilliseconds() >> =C2=A0 =C2=A0 { >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 // This method is protected >> =C2=A0 =C2=A0 =C2=A0 =C2=A0 $this->seconds =3D $value * 3600; >> =C2=A0 =C2=A0 } >> } >> >> For me, the advantage here is that I can independently the methods >> from the property. If I want to force a subclass to implement a >> setter/getter, then I can abstract the function in the base class. >> Sure, some may say that I should be using an interface. I disagree as >> I probably don't want the methods to be public. Protected or even >> private and/or final. > > In the syntax I provided in my RFC, it is certainly possible to define a > property with only a get or only a set method (these are implicit > read-only and write-only properties). =C2=A0Furthermore, it is also possi= ble to > set the visibility of the get and set methods individually, as well as > making either one final, static or (and I forgot to mention this in the > RFC) abstract. =C2=A0But the advantage of my syntax, is not only can thes= e > things be set individually, but they can also be set just once for the > pair, by specifying them on the property itself. =C2=A0This makes for cle= aner > and more readable code. > > My syntax also gives several other advantages over the delphi syntax. =C2= =A0It > is more logical, as it makes the property look more like a class variable > than a class method. =C2=A0This makes sense because you call it like a > variable. =C2=A0Additionally, because the get/set methods need to be cont= ained > within the body of the property definition, you immediately know if a > property has both a get and a set method at a quick glance - you do not > have to hunt through the class to see if there is another definition > somewhere else. > > >> The classic example is one of shapes. Every shape has a public $area >> property, but the value would be provided by an abstract protected >> TShape::getArea(); method. I can also finalise them, so, for example, >> a triangle shape could have a final protected getArea() method and all >> sub classes of triangles (scalene, isosceles, equilateral) would not >> implement their own getArea() method. > > This is possible with the syntax I provided. =C2=A0I would suggest readin= g more > about the C# syntax, which my suggested syntax is based off of, as it wil= l > explain all of your questions. > > http://msdn.microsoft.com/en-us/library/x9fsa0sw%28VS.80%29.aspx > > - Dennis > > Thanks for your reply. Fundamentally, a big +1 from my little voice on having setters/getters in P= HP. The issue of documentation is probably that the documentation tools would have to adapt. As things stand PHPDoc doesn't support namespaces, so setters/getters would just be added to the WIBNI list. With regard to the value supplied to the set method, would it make more sense for PHP to be ... set($value) { $this->seconds =3D $value * 3600; } or set { $this->seconds =3D __SETVALUE__ * 3600; } Having $value without a clear indication of where it comes from doesn't read quite right. $value is way to generic to be magically created. __SETVALUE__ (or __SOMETHINGELSE__) is clear in this regard. Richard. --=20 Richard Quadling Twitter : EE : Zend @RQuadling : e-e.com/M_248814.html : bit.ly/9O8vFY