Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:50724 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 35967 invoked from network); 30 Nov 2010 12:48:18 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 30 Nov 2010 12:48:18 -0000 Authentication-Results: pb1.pair.com smtp.mail=president@basnetworks.net; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=president@basnetworks.net; sender-id=unknown; domainkeys=good Received-SPF: error (pb1.pair.com: domain basnetworks.net from 208.97.132.202 cause and error) DomainKey-Status: good X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: president@basnetworks.net X-Host-Fingerprint: 208.97.132.202 caiajhbdccac.dreamhost.com Linux 2.6 Received: from [208.97.132.202] ([208.97.132.202:52324] helo=homiemail-a15.g.dreamhost.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 08/00-35426-092F4FC4 for ; Tue, 30 Nov 2010 07:48:17 -0500 Received: from homiemail-a15.g.dreamhost.com (localhost [127.0.0.1]) by homiemail-a15.g.dreamhost.com (Postfix) with ESMTP id 5C72376C072; Tue, 30 Nov 2010 04:48:13 -0800 (PST) DomainKey-Signature: a=rsa-sha1; c=nofws; d=basnetworks.net; h=message-id :in-reply-to:references:date:subject:from:to:cc:mime-version :content-type:content-transfer-encoding; q=dns; s= basnetworks.net; b=3bp6sVh2u3MvyI5lDrGbmpdfVAYF202b3Aaspy0ktq4PJ BiLjT5WOzsyfJU6AWOHCg7n/+Y195G4oUNFbHTEPOu86el/MZXfU/v6f/B2PTM0o GbFXGvtdqOQV8Y0B4pvmy4ucvu3jmazbUv13xJ48/dPB3/niTpLULCcair1a0k= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=basnetworks.net; h= message-id:in-reply-to:references:date:subject:from:to:cc :mime-version:content-type:content-transfer-encoding; s= basnetworks.net; bh=BDV+oY2q584uAReWqWggfnvdefE=; b=M4BLNTe4DRRY auTcWPX1m6htTegG9m4c62Wu+B60o+rCv9IJpIgMdoxh58lUdyU8zEnOb8SMD8t7 QqNvsjCxlErDgU4BRcGEEFcHkto6Mf0y/0diWDkqOhz0BAt7s7OlDsngelwN0wGA Og9TCErZPA7vKFYEl52xsT+4uyGRf+8= Received: from webmail.basnetworks.net (caiajhbdcbhh.dreamhost.com [208.97.132.177]) (Authenticated sender: president@basnetworks.net) by homiemail-a15.g.dreamhost.com (Postfix) with ESMTPA id 1DF5E76C06F; Tue, 30 Nov 2010 04:48:13 -0800 (PST) Received: from 70.28.48.126 (proxying for 70.28.48.126) (SquirrelMail authenticated user president@basnetworks.net) by webmail.basnetworks.net with HTTP; Tue, 30 Nov 2010 07:48:13 -0500 Message-ID: <073c59f255a348e9684fb4875490b23f.squirrel@webmail.basnetworks.net> In-Reply-To: References: Date: Tue, 30 Nov 2010 07:48:13 -0500 To: RQuadling@googlemail.com Cc: internals@lists.php.net User-Agent: SquirrelMail/1.4.21 MIME-Version: 1.0 Content-Type: text/plain;charset=iso-8859-1 Content-Transfer-Encoding: quoted-printable Subject: Re: [PHP-DEV] RFC: C-sharp style property get/set syntax for PHP From: president@basnetworks.net 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. What I hav= e 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. Looking at a compiled C# library with reflector will reveal this underlying implementation. So when a cal= l to a property is compiled in C#, it is simple replaced with a call to a method. The properties themselves are nothing more than syntactic sugar. 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.=20 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 > { > protected $seconds; > > public property Hours read getHours write setHours; > > protected function getHours() > { > return $this->seconds / 3600; > } > > protected function setHours() > { > $this->seconds =3D $value * 3600; > } > > // This property is read-only > public property Minutes read getMinutes; > > protected function getMinutes() > { > return $this->seconds / 60; > } > > public property Milliseconds read getMilliseconds write > setMilliseconds; > > public function getMilliseconds() > { > // This method is public > return $this->seconds * 60; > } > > protected function setMilliseconds() > { > // This method is protected > $this->seconds =3D $value * 3600; > } > } > > 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). Furthermore, it is also possible t= o 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. But the advantage of my syntax, is not only can these things be set individually, but they can also be set just once for the pair, by specifying them on the property itself. This makes for cleaner and more readable code. My syntax also gives several other advantages over the delphi syntax. It is more logical, as it makes the property look more like a class variable than a class method. This makes sense because you call it like a variable. Additionally, because the get/set methods need to be contained 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. I would suggest reading mor= e 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