<?php class A { private $a; public function __construct($a) { $this->a = $a; } } class B extends A { /** * Use php's magic method to enforce immutability */ public function __set($property, $value) { throw new RuntimeException(self::class . ' is immutable'); } } $a = new A(1); $b = new B(2); $a->b = $b; var_dump($a); try { $b->c = 3; } catch (RuntimeException $re) { var_dump($b); }
You have javascript disabled. You will not be able to edit any code.