3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Foo { private $data = 'some data'; } class Bar { private readonly string $data; public function __construct(string $data) { $this->data = $data; } } $foo = new Foo(); $bar = new Bar('some data'); (fn() => $this->data = 'other data')->call($foo); //(fn() => $this->data = 'other data')->call($bar); $foo2 = (fn() => $this->data)->call($foo); //$bar2 = (fn() => $this->data)->call($bar); echo $foo2; //echo $bar2;
Output for 8.1.9 - 8.1.33, 8.2.25 - 8.2.29, 8.3.5 - 8.3.26, 8.4.1 - 8.4.13
other data

preferences:
53.87 ms | 406 KiB | 5 Q