3v4l.org

run code in 300+ PHP versions simultaneously
<?php interface EntityInterface { } class EntityDecorator implements EntityInterface { public $subject; public function __construct(EntityInterface $subject) { $this->subject = $subject instanceof static ? $subject->getEntity() : $subject; } public function getEntity() { return $this->subject; } } class Entity implements EntityInterface {} class DecoratorA extends EntityDecorator {} class DecoratorB extends EntityDecorator {} $entity = new Entity(); var_dump($entity); $a = new DecoratorA($entity); var_dump($a); $b = new DecoratorB($a); var_dump($b); $b2 = new DecoratorB($b); var_dump($b2); $a2 = new DecoratorA($b2); var_dump($a2);

preferences:
43.35 ms | 402 KiB | 5 Q