Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79180 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 25568 invoked from network); 25 Nov 2014 21:26:23 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 25 Nov 2014 21:26:23 -0000 Authentication-Results: pb1.pair.com smtp.mail=dev@mabe.berlin; spf=permerror; sender-id=unknown Authentication-Results: pb1.pair.com header.from=dev@mabe.berlin; sender-id=unknown Received-SPF: error (pb1.pair.com: domain mabe.berlin from 80.237.132.167 cause and error) X-PHP-List-Original-Sender: dev@mabe.berlin X-Host-Fingerprint: 80.237.132.167 wp160.webpack.hosteurope.de Received: from [80.237.132.167] ([80.237.132.167:58669] helo=wp160.webpack.hosteurope.de) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id 2C/BD-40624-DF3F4745 for ; Tue, 25 Nov 2014 16:26:22 -0500 Received: from dslb-088-074-069-014.088.074.pools.vodafone-ip.de ([88.74.69.14] helo=[192.168.178.30]); authenticated by wp160.webpack.hosteurope.de running ExIM with esmtpsa (TLS1.0:DHE_RSA_AES_128_CBC_SHA1:16) id 1XtNco-0007AI-ST; Tue, 25 Nov 2014 22:26:18 +0100 Message-ID: <5474F3FA.8050108@mabe.berlin> Date: Tue, 25 Nov 2014 22:26:18 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:31.0) Gecko/20100101 Thunderbird/31.2.0 MIME-Version: 1.0 To: PHP internals Content-Type: multipart/alternative; boundary="------------070806060105060900060208" X-bounce-key: webpack.hosteurope.de;dev@mabe.berlin;1416950782;82d3cd2b; Subject: Idea: immutable class / object From: dev@mabe.berlin (Marc Bennewitz) --------------070806060105060900060208 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Hi internals, In OOP it's a sometimes a common issue to know the state of an object and to know if it was changed respectively it it can change state. We already have such objects like DateTImeImmutable and new proposed objects like UString also introduce immutable objects. I think it would be really helpful to see this kind of issue addressed in a standardized way: if you approve with this I would write down a more structured RFC. Now see the following code snipped: ts = time(); $this->setObj($obj); } public function getTimestamp() { return $this->ts; } public function getObj() { return $this->obj; } public function setObj($obj) { $this->obj = $obj; } } // initialize $obj = new DateTImeImmutable(); $obj = new MyImmutableClass($obj); // check if immutable var_dump(MyImmutableClass::immutable); var_dump($obj::immutable); // read properties var_dump($obj->ts, $obj->obj); var_dump($obj->getTimestamp(), $obj->getObj()); // ERROR: Immutable objects can not contain mutable properties $obj = new MyImmutableClass(new stdClass); // ERROR: Immutable objects can not be changed after initialization $obj = new MyImmutableClass(); $obj->setObj($obj); $obj->obj = $obj; // ERROR: It's not allowed to change immutable variables MyImmutableClass::$staticProperty = 'new value'; ?> Because of for immutable objects it's not allowed to set an mutable object as property you can be sure the object's state will be consistent. That's the main difference from marking all properties readonly or similar functionalities. Additionally it's simple to test if an class/object is immutable and document it for free. Downside: New Keyword "immutable" // --------------070806060105060900060208--