Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:65127 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 35806 invoked from network); 23 Jan 2013 21:17:28 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 23 Jan 2013 21:17:28 -0000 Authentication-Results: pb1.pair.com header.from=morrison.levi@gmail.com; sender-id=pass Authentication-Results: pb1.pair.com smtp.mail=morrison.levi@gmail.com; spf=pass; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.175 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.214.175 mail-ob0-f175.google.com Received: from [209.85.214.175] ([209.85.214.175:37877] helo=mail-ob0-f175.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 9F/B4-30997-76350015 for ; Wed, 23 Jan 2013 16:17:28 -0500 Received: by mail-ob0-f175.google.com with SMTP id uz6so287027obc.6 for ; Wed, 23 Jan 2013 13:17:25 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:cc:content-type; bh=+m3sR8YGKxm6MSle54y0264oOPrkeeJpQ9KFXw5sirs=; b=XxpNdhjc8WeGe5/ikpreqoC8PSfJ3Ds30r8lz+OfM9jVn72+JzLUQv90kfrY0jfcFY mEwj7X6ApxPt0HRgBJbq5B9HFDIZx9QpTEewlAgqb6AsR/3ksHdwY56bpBZuJypmK6gf MwybNhMGIdcjTHh9HH9+BC7wroXtmPc8JH05De1lSO08a7aAdgaFfs/Fk22Zp8S1aN+o E+2vTbyP55z9SUhcCsxYAcPJcGqag+YtrMSIbyihakiea1KQvsoJ7t8YEPgr5+WwNK4b hPjf81ImSVylK36no8ZxBaU4ZK1ACKWwMNT5hnMxtgUty7MmXwoR9R8IPpFW+zBjpeMc jQ8g== MIME-Version: 1.0 X-Received: by 10.182.42.97 with SMTP id n1mr2137430obl.91.1358975845023; Wed, 23 Jan 2013 13:17:25 -0800 (PST) Received: by 10.76.82.234 with HTTP; Wed, 23 Jan 2013 13:17:24 -0800 (PST) In-Reply-To: References: <510038C9.5000900@mrclay.org> Date: Wed, 23 Jan 2013 14:17:24 -0700 Message-ID: To: Clint Priest Cc: Steve Clay , PHP Internals Content-Type: text/plain; charset=ISO-8859-1 Subject: Re: [PHP-DEV] C# properties vs. accessors RFC From: morrison.levi@gmail.com (Levi Morrison) Steve: Like your summary. Disagree with a few points but in general I agree with you. More below. > 2. C# has no issetter/unsetter. > > IMO customizing these functions is completely unneeded for the vast majority > of use cases and could be replaced by simpler logic: isset($this->prop) > calls the getter and compares to NULL; unset($this->prop) passes NULL to the > setter. Agreed, but if they are automatically generated then I see no harm in allow custom `isset` and `unset` behavior as long as it doesn't get in the way or complicate things. > I think the path forward is to determine how we can serve the same goals as > this RFC, but retain the conceptual simplicity of the C# implementation and > maybe syntax that strays less from current PHP. > > I have some ideas that I could start forging into an RFC. Consider this: > > class Foo { > private $_bar; > public function get bar { return $this->_bar; } > public function set bar { $this->_bar = $value; } > } > > Advantages I see over the RFC: > * It's clearer that the accessors map to regular methods, and you know how > to control visibility of methods and how they're inherited. > * It's clearer $bar doesn't exist as a property, and it will never show up > in state inspection. > * You know $_bar is a plain PHP property, and you can initialize it directly > in the constructor; we don't need an "initter". > * There are no guards/proxies/shadow property to think about Keyword 'function' is unnecessary and please don't use a magic `$value`. Just do a normal parenthesis: class Foo { private $_bar; public get bar() { return $this->_bar; } public set bar($value) { $this->_bar = $value; } } This allows for a function-like feel and type-hints as well. Which brings me to the shorthand notation: > As for type-hinting, I think that could be achieved by a separate, simpler > mechanism: type-hinting the properties. > > class Foo { > private Bar? $_bar; > } I like type-hinting properties as a shorter syntax, but now we have a `private` method that has public getters/setters? Seems odd at first. In any case it is confusing. I also don't like the `?` for `nullable`. Just stick with PHP convention and do: class Foo { public Bar $bar = NULL; } Ralph: >This is too similar to what we have today: > >class Foo { > private $_bar; > public function getBar() { return $this->_bar; } > public function setBar($value) { $this->_bar = $value; } > >} Is there anything inherently *wrong* with that method? And the fact that it is close is a GOOD indication, right? It's familiar and easy to adopt. In fact, if we did accessors annotations style (which Python has, so don't knock it blindly): class Foo { private $bar; @bar.getter public function getBar() { return $this->bar; } @bar.setter public function setBar($value) { $this->bar = $value; } } Then I'd say that this is *perfect* style. All we add is some annotations to allow it to be used in getter and setter syntax: $foo = new Foo; $foo->bar = 2; //Foo::setBar $bar = $foo->bar; //Foo::getBar I can name my getters and setters however I want. Talk about about options! Note: I'm not saying we should use annotations for this; I'm just saying it makes certain cases nicer. I'm sure we can find drawbacks as well. Discuss at will. Clint: I'm sorry that you spent all that time without hearing feedback from a lot of the "No" voters. Had they been participating all along perhaps it could have been avoided. We'll never know.