# Idea: immutable class / object

- Thread: https://externals.io/message/79180
- Original archive: https://news-web.php.net/php.internals/79180
- Started by Marc Bennewitz on 2014-11-25T21:26:18+00:00
- Messages: 5
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · RFCs at https://externals.io/rfc.md · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #79180 — Marc Bennewitz — 2014-11-25T21:26:18+00:00

URL: https://externals.io/message/79180#79180

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:

<?php

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"

/<http://dict.leo.org/#/search=adv.&searchLoc=0&resultOrder=basic&multiwordShowSingle=on>/

---

## #79244 — Marc Bennewitz — 2014-11-27T19:19:22+00:00

URL: https://externals.io/message/79180#79244 · In reply to: #79180

Thoughts ?

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:
>
> <?php
>
> 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"

---

## #79246 — Levi Morrison — 2014-11-27T19:47:42+00:00

URL: https://externals.io/message/79180#79246 · In reply to: #79244

> Am 25.11.2014 um 22:26 schrieb Marc Bennewitz:
>
> > […]


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.

---

## #79251 — Marc Bennewitz — 2014-11-27T20:17:02+00:00

URL: https://externals.io/message/79180#79251 · In reply to: #79246

Am 27.11.2014 um 20:47 schrieb Levi Morrison:
> > […]
>
> 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.
>
Are you sure there are so much places and runtime checks required for 
immutable classes?
It's only needed to test on immutable objects and such objects doesn't 
do it as it's not possible to do.
I think of flag ZEND_ACC_IMMUTABLE + simply switch the internal default 
function used to write properties to an object but I don't have enough 
knowledge about the engine.

Marc

---

## #79260 — Stanislav Malyshev — 2014-11-28T06:46:11+00:00

URL: https://externals.io/message/79180#79260 · In reply to: #79180

Hi!

> 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

It may be harder to achieve than you think. How exactly do you ensure
they property values do not end up containing mutable values?
I.e. something like this in the ctor:

$a =& $this->foo;
$a = bar();

How do you ensure $this->foo does not have mutable value in it now?
Also, would that mean that you can not call any function that accepts
values by-ref with anything derived from $this? It may be not easy to do.

Also, I'm not sure how practical it is. I.e., immutability is very
imporant in some applications like parallel processing and also for some
languages where some performance optimizations rely very hard on certain
constructs being immutable, but in PHP there are no such things, so
except for documentation, what would be the added value here?
-- 
Stas Malyshev
smalyshev@gmail.com
