Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:64503 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 40134 invoked from network); 3 Jan 2013 15:33:52 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 3 Jan 2013 15:33:52 -0000 Authentication-Results: pb1.pair.com smtp.mail=steve@mrclay.org; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=steve@mrclay.org; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mrclay.org from 50.22.11.19 cause and error) X-PHP-List-Original-Sender: steve@mrclay.org X-Host-Fingerprint: 50.22.11.19 bedford.accountservergroup.com Linux 2.6 Received: from [50.22.11.19] ([50.22.11.19:46249] helo=bedford.accountservergroup.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 84/79-35624-FD4A5E05 for ; Thu, 03 Jan 2013 10:33:51 -0500 Received: from n128-227-98-14.xlate.ufl.edu ([128.227.98.14]:54123 helo=Distance-Ed-Sclay.local) by bedford.accountservergroup.com with esmtpsa (TLSv1:DHE-RSA-AES256-SHA:256) (Exim 4.80) (envelope-from ) id 1Tqmnk-0008jJ-BG for internals@lists.php.net; Thu, 03 Jan 2013 09:33:48 -0600 Message-ID: <50E5A4DB.2000205@mrclay.org> Date: Thu, 03 Jan 2013 10:33:47 -0500 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:17.0) Gecko/17.0 Thunderbird/17.0 MIME-Version: 1.0 To: internals@lists.php.net References: <50E41BB6.4030901@zerocue.com> <50E4A43E.6030302@zerocue.com> <50E4B232.5000505@mrclay.org> <50E4BDDE.8050509@zerocue.com> <50E4D0BB.7060701@mrclay.org> <50E4FA09.7030001@zerocue.com> <50E529B6.1050903@sugarcrm.com> <50E56450.1060801@zerocue.com> In-Reply-To: <50E56450.1060801@zerocue.com> Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - bedford.accountservergroup.com X-AntiAbuse: Original Domain - lists.php.net X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - mrclay.org Subject: Re: [PHP-DEV] [PHP-RFC] Property Accessors 1.2 for Final Review before Vote From: steve@mrclay.org (Steve Clay) On 1/3/13 5:58 AM, Clint Priest wrote: > class Foo { > public $bar = 2 { > get; > set; > } > } Consider properties not based on shadowed values: class Foo { private $realbar; public $bar = 2 { get { return $this->realbar; } set { $this->realbar = $value; } } } Here, initializing the shadow property is useless. It's similar to the case where you want to have the initial value of a traditional property be the result of an expression (and you can't because it would create a chicken-egg problem during construction). Option 1: The most powerful solution I see is to have an init function that's implicitly called before first access (and with direct write access to the shadow property if needed). Consider trying to emulate the crazy document.cookie API: class Document { private $cookieProps; public $cookie { get { return /* based on cookieProps */; } set { /* set some cookieProps */ } // called implicitly before the first access (not before the constructor runs) // and would have direct access to the shadow property. init { /* set up cookieProps */ } } } Pros: can run any code necessary to initialize the property Cons: must make a function just to have a default Option 2: Keep the traditional syntax, but instead of always setting the shadow property, call the setter with the given initial value directly before the first access. Pros: familiar syntax; more expected behavior in some cases Cons: what if there's no setter?; cannot run arbitrary setup code; setter can't distinguish between a "user" set and the implicitly set before first access. Option 3: A mix of both. The shadow property is always initialized with the value from the traditional syntax. If needed, you can have an init that's called before first access. Pros: For simple shadowed properties, just works as you'd expect and with familiar syntax; Still allows robust initialization before first access if needed. Cons: Author must remember that traditional syntax only useful for shadow prop. Steve Clay -- http://www.mrclay.org/