Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:79246 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 43230 invoked from network); 27 Nov 2014 19:47:45 -0000 Received: from unknown (HELO lists.php.net) (127.0.0.1) by localhost with SMTP; 27 Nov 2014 19:47:45 -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.218.49 as permitted sender) X-PHP-List-Original-Sender: morrison.levi@gmail.com X-Host-Fingerprint: 209.85.218.49 mail-oi0-f49.google.com Received: from [209.85.218.49] ([209.85.218.49:34940] helo=mail-oi0-f49.google.com) by pb1.pair.com (ecelerity 2.1.1.9-wez r(12769M)) with ESMTP id CB/A8-27910-1EF77745 for ; Thu, 27 Nov 2014 14:47:45 -0500 Received: by mail-oi0-f49.google.com with SMTP id i138so3827987oig.22 for ; Thu, 27 Nov 2014 11:47:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:sender:in-reply-to:references:date:message-id:subject :from:to:cc:content-type; bh=dxSrR486RvJI9qsSjpbQduVLeftGGCW7paBnlVvkFsE=; b=g2tBf7EoOVnykwiEq4/XsXAyQUMakqdK6nP6kQvEDnb5R+TQKUJuSKYvl9eIcdW8w7 VKWMu19rELLZ35PC+jSnNKO6hDjKbq0tKD3SC0r/s3TUXri/oSJkySzdFmzGfFotXhTd ekUYOwbnauLYYTnk5ViW8dOnnfTtC+j+YqfVL6SdCHWsUzmd7K7ShgBVwUKqRcenNads OTW0U1hGjFlInOnAXvS9pVOnp06+Zla49bmeQTztGAa9axwcAjzOtfY5B1b3QN7aN7lW HpaqzPJtcZY8nYbVQgzBqW9DL3r7iJ3znKWtAEbJMOuQKhgr8V6Q5BLkn4P5L8KTESU0 2LQA== MIME-Version: 1.0 X-Received: by 10.202.45.198 with SMTP id t189mr22340368oit.68.1417117662435; Thu, 27 Nov 2014 11:47:42 -0800 (PST) Sender: morrison.levi@gmail.com Received: by 10.76.89.237 with HTTP; Thu, 27 Nov 2014 11:47:42 -0800 (PST) In-Reply-To: <5477793A.1010307@mabe.berlin> References: <5474F3FA.8050108@mabe.berlin> <5477793A.1010307@mabe.berlin> Date: Thu, 27 Nov 2014 12:47:42 -0700 X-Google-Sender-Auth: EISxcrcJmLKszE4fOPYjUN8eVSg Message-ID: To: Marc Bennewitz Cc: internals Content-Type: text/plain; charset=UTF-8 Subject: Re: [PHP-DEV] Idea: immutable class / object From: levim@php.net (Levi Morrison) > Am 25.11.2014 um 22:26 schrieb Marc Bennewitz: > >> 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: >> >> > >> immutable class MyImmutableClass { >> public static $staticProperty = 'private static'; >> public $ts; >> public $obj; >> >> public function __construct($obj = null) { >> $this->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" What I think is more useful is C++'s const, which basically makes any structure immutable when you need it. However, I don't think it's worth going through the effort to bring this to PHP, as you would have to add checks for preventing state changes in many places at runtime.