Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:50692 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 37781 invoked from network); 29 Nov 2010 14:30:05 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 29 Nov 2010 14:30:05 -0000 Authentication-Results: pb1.pair.com smtp.mail=keisial@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=keisial@gmail.com; sender-id=pass; domainkeys=bad Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.215.170 as permitted sender) DomainKey-Status: bad X-DomainKeys: Ecelerity dk_validate implementing draft-delany-domainkeys-base-01 X-PHP-List-Original-Sender: keisial@gmail.com X-Host-Fingerprint: 209.85.215.170 mail-ey0-f170.google.com Received: from [209.85.215.170] ([209.85.215.170:61979] helo=mail-ey0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id D6/A4-09925-CE8B3FC4 for ; Mon, 29 Nov 2010 09:30:04 -0500 Received: by eyf5 with SMTP id 5so1936169eyf.29 for ; Mon, 29 Nov 2010 06:30:00 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:received:message-id:date:from :user-agent:mime-version:to:cc:subject:references:in-reply-to :content-type:content-transfer-encoding; bh=+juYJVB3jfTWzZ7v/1cby3dOAAzxdr3i0IdaPU402ME=; b=cXN1m/NV82eu13b/0ux8F48ePbodNRCkSVLnzdNpU9PHtfdI1br8SJxIUaUonvaf+j pcM5pT/AJde5hQRgiakKue1QGIUtd0+ioNILOWd8E5BqaulxAgYTGxwQjh75krfjGWUM CyppJmLOCa3fNZs1cgq7jz7XmQ6Eb7bTUXTno= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=message-id:date:from:user-agent:mime-version:to:cc:subject :references:in-reply-to:content-type:content-transfer-encoding; b=X6W44vXo1tDjac/7FtJzn/qwbw5tjVf9gsqv2ngyoj1hfeLOrnmW7Li3ui6VZJ9uPJ sWqxFwF3lMWKXRdUm8G5muG26z7nOP54rki8pBA4JFPdLYC/B5g/yuF/wQBSga+5VRml 7fp9fDErB0cweTPz+5Cw4Iy3ZMc4cQ27QzL/g= Received: by 10.204.59.81 with SMTP id k17mr4547197bkh.148.1291041000176; Mon, 29 Nov 2010 06:30:00 -0800 (PST) Received: from [192.168.1.26] (23.Red-83-35-1.dynamicIP.rima-tde.net [83.35.1.23]) by mx.google.com with ESMTPS id p22sm2066372bkp.9.2010.11.29.06.29.58 (version=SSLv3 cipher=RC4-MD5); Mon, 29 Nov 2010 06:29:59 -0800 (PST) Message-ID: <4CF3B903.6000204@gmail.com> Date: Mon, 29 Nov 2010 15:30:27 +0100 User-Agent: Thunderbird MIME-Version: 1.0 To: RQuadling@googlemail.com CC: internals@lists.php.net References: In-Reply-To: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit Subject: Re: [PHP-DEV] RFC: C-sharp style property get/set syntax for PHP From: keisial@gmail.com ("=?UTF-8?B?w4FuZ2VsIEdvbnrDoWxleg==?=") Richard Quadling wrote: > 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. > > 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 = $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 = $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. > > 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. > > The downside is certainly that the code is more verbose than I would > guess many people would like. > > Regards, > > Richard. > > [1] http://www.delphibasics.co.uk/RTL.asp?Name=Property setMilliseconds() should have $value as parameter instead of a magic name. What about allowing this syntax to attach the property to a variable? For instance: = 0 && $seconds < 60) $this->seconds = $seconds; } public function setMinutes($minutes) { if ($minutes >= 0 && $minutes < 60) $this->minutes = $minutes; } public function setHours($hours) { if ($hours >= 0 && $hours < 24) $this->hours = $hours; } } We only want to perform checks on write, so instead of writing the trivial getters, the property is set to the variable itself. Child classes could reattach it to a function if needing more control.