Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:61246 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 67466 invoked from network); 15 Jul 2012 15:46:32 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 15 Jul 2012 15:46:32 -0000 Authentication-Results: pb1.pair.com smtp.mail=amaury.bouchard@gmail.com; spf=pass; sender-id=pass Authentication-Results: pb1.pair.com header.from=amaury.bouchard@gmail.com; sender-id=pass Received-SPF: pass (pb1.pair.com: domain gmail.com designates 209.85.214.170 as permitted sender) X-PHP-List-Original-Sender: amaury.bouchard@gmail.com X-Host-Fingerprint: 209.85.214.170 mail-ob0-f170.google.com Received: from [209.85.214.170] ([209.85.214.170:58406] helo=mail-ob0-f170.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id E0/AB-20866-6D5E2005 for ; Sun, 15 Jul 2012 11:46:31 -0400 Received: by obfk16 with SMTP id k16so9589141obf.29 for ; Sun, 15 Jul 2012 08:46:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:from:date:x-google-sender-auth:message-id :subject:to:content-type; bh=gdIBLF9jndhZcLsouzPGUdc6r3xpzPGFSLSfJIm5vv4=; b=cTcU/03+SfWiyiKJeyqjWR3S6ogawffC/UHUWWyN4+5CTwUQZXWX7fsCFHpEsGl7D3 LanUHjlHVpcYlPVw8I8zjnSCGgLK2C7r40V3u4F2RvhT0yxeJ5yhKdRBsUa3awmuACNd M869NEAWBbJYf1YsV7Am9HK9u0hi5sYeWJ9WubPTpJx4lY+hzhUdU9IDyLP2NF9YRC1Q HJTeMPPQRu0ERD+C/4FYCeXS8h9asPk8Qy1VbxZU8UI4acFRImlKnZLEu6tm+BhfUqoY Rmp1DVyoQN9yasw4pxSLonh7ViuhU9gAM5NJ2ZGcjTNxiCVxBHAxDlZPL/PrQ2qVroam ya0w== Received: by 10.182.164.40 with SMTP id yn8mr3406924obb.40.1342367188255; Sun, 15 Jul 2012 08:46:28 -0700 (PDT) MIME-Version: 1.0 Sender: amaury.bouchard@gmail.com Received: by 10.182.60.131 with HTTP; Sun, 15 Jul 2012 08:46:08 -0700 (PDT) Date: Sun, 15 Jul 2012 17:46:08 +0200 X-Google-Sender-Auth: 6a-3pJlx12-5Jbc40zq-Xz7l7AI Message-ID: To: internals@lists.php.net Content-Type: multipart/alternative; boundary=e89a8f642fee6a042804c4e03757 Subject: RFC Proposal - Attributes read/write visibility From: amaury@amaury.net (Amaury Bouchard) --e89a8f642fee6a042804c4e03757 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable Hi, Here is an RFC proposal about a syntax extension for PHP. The purpose is to manage precisely the visbiliy of attributes, by separating reading and writing access. First of all, I know there is already an RFC about attributes ("Property get/set syntax" [1]). Its goal is mainly different, but I'll discuss it lower. THE PROBLEM In my experience, one of the major usage of getters is when you want to have an attribute, readable (but not writable) from outside the object. More, the attribute's visibilty is then chosen between private and protected, depending on your inheritance design. The result is not really satisfying: 1. You have to write getter's code. Maybe, it's just a simple return, but every line of code should be useful, not a workaround. 2. You ended with 2 syntaxes; $obj->attr when the attribute is public, but $obj->getAttr() when you must use a getter. It's a bit schizophrenic. THE IDEA I suggest to be able to separate reading visibility and writing visibility, using a colon to separate them. If only one visibility is given, it will be used for both reading and writing access. For example: class A { public $a; // public reading, public writing public:public $b; // the same public:protected $c; // public reading, protected writing public:private $d; // public reading, private writing public:const $e; // public reading, no writing allowed protected $f; // protected reading, protected writing protected:protected $g; // the same protected:private $h; // protected reading, private writing protected:const $i; // protected reading, no writing allowed private $j; // private reading, private writing private:private $k; // the same private:const $l; // private reading, no writing allowed } As you can see, I think that writing access should be more restrictive than reading access (or equivalent to it). A "private:public" visibility would make no sense. SOURCE CODE I did a patch. It kinda works, but I'm still working on it (because I'm still learning Zend Engine's internals). The code on GitHub: https://github.com/Amaury/php-src All advises are welcome. PRIOR WORK As I said before, the "Property get/set syntax" RFC is in discussion. My proposal doesn't focus on the same goals, but they could be fully compatible. Thus, we would be able to write this kind of code: class A { // $str has public reading and private writing, // and manage french quotes public:private $str { get { return "=AB" . $this->str . "=BB"; } set { $this->str =3D trim($value, "=AB=BB"); } } } Maybe you saw that the "Property get/set syntax" RFC has an intended syntax for read-only and write-only attributes. From my point of view, there is a deep and clean separation between a getter/setter syntax and an extended visibility syntax. It shouldn't be in the same RFC. More, the proposed "read-only" and "write-only" keywords are less precise and powerful than what I'm suggesting. I would be happy to discuss all that with you guys. Best regards, Amaury Bouchard [1] : https://wiki.php.net/rfc/propertygetsetsyntax-as-implemented --e89a8f642fee6a042804c4e03757--