Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:56785 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 12442 invoked from network); 5 Dec 2011 23:31:02 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 5 Dec 2011 23:31:02 -0000 Authentication-Results: pb1.pair.com smtp.mail=rasmus@mindplay.dk; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=rasmus@mindplay.dk; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mindplay.dk from 209.85.161.170 cause and error) X-PHP-List-Original-Sender: rasmus@mindplay.dk X-Host-Fingerprint: 209.85.161.170 mail-gx0-f170.google.com Received: from [209.85.161.170] ([209.85.161.170:64607] helo=mail-gx0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id ED/20-09469-58F4DDE4 for ; Mon, 05 Dec 2011 18:11:01 -0500 Received: by ggnv1 with SMTP id v1so5304764ggn.29 for ; Mon, 05 Dec 2011 15:10:58 -0800 (PST) MIME-Version: 1.0 Received: by 10.182.73.71 with SMTP id j7mr2165729obv.55.1323126658435; Mon, 05 Dec 2011 15:10:58 -0800 (PST) Received: by 10.182.44.130 with HTTP; Mon, 5 Dec 2011 15:10:58 -0800 (PST) Date: Mon, 5 Dec 2011 18:10:58 -0500 Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=f46d044480d7780b7104b3606e3c Subject: Re: Patch: getters/setters syntax Implementation From: rasmus@mindplay.dk (Rasmus Schultz) --f46d044480d7780b7104b3606e3c Content-Type: text/plain; charset=ISO-8859-1 2011/12/4 Clint M Priest : > Updated patch w/o white-space: http://www.clintpriest.com/patches/accessors_v1.patch > > In the end it is a relatively simple patch. The new syntax effectively creates internal functions on the object and the system looks for those functions and calls them at the appropriate time. > > Example: > class z { > public $Hours { > public get { return $this->_Hours; } > protected set { $this->_Hours = $value; } > } > } > > Defines: > $o->__getHours(); > $o->__setHours($value); You forgot to declare the backing field z::$_Hours in this example. From a semantic point of view, I find it misleading to first declare $Hours as public, then lowering the bar by making the set-accessor protected. The most common use-case for accessors is public - so I would suggest a syntax more along the lines of this: class z { private $_hours; get $hours { // accessor is public by default return $this->_hours; } protected set $hours { $this->_hours = $hours; // local variable $hours is the new value } } And perhaps a short form for added convenience, where the backing-field is automatically added for you - e.g.: class z { get $hours { // accessor is public by default return $value; // $value provides access to the backing field (same way $this provides access to the object context) } protected set $hours { $value = $hours; // local variable $hours is the new value, $value references the backing field } } thoughts? --f46d044480d7780b7104b3606e3c--