3v4l.org

run code in 300+ PHP versions simultaneously
<?php trait OptionzTrait { protected $_options = array(); public function setOptions(array $options) { $this->_options = array_replace_recursive( $this->_options, $options ); } public function getOptions() { return $this->_options; } } class Optionz { use OptionzTrait; protected $_inception; public function __construct($options) { $this->_options = array( 'fact' => null, 'inception' => array( 'options' => array() ) ); $this->setOptions($options); } public function stateFact() { echo $this->_options['fact'] . "\n"; } public function getInception() { if ($this->_inception === null) { $this->_inception = new static($this->_options['inception']['options']); } return $this->_inception; } public function setInception(Optionz $inception) { $this->_inception = $inception; } } $optionz = new Optionz(array('inception' => array('options' => array('fact' => 'it is known')))); $optionz->getInception()->stateFact(); // caveat: nested properties are only propagated initially $optionz->getInception()->setOptions(array('fact' => 'this works')); $optionz->getInception()->stateFact(); $optionz->setInception(null); // try this $optionz->setOptions(array('inception' => array('options' => array('fact' => 'this doesnt')))); $optionz->getInception()->stateFact();
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
it is known this works Fatal error: Uncaught TypeError: Optionz::setInception(): Argument #1 ($inception) must be of type Optionz, null given, called in /in/elfOH on line 55 and defined in /in/elfOH:43 Stack trace: #0 /in/elfOH(55): Optionz->setInception(NULL) #1 {main} thrown in /in/elfOH on line 43
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.25, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
it is known this works Fatal error: Uncaught TypeError: Argument 1 passed to Optionz::setInception() must be an instance of Optionz, null given, called in /in/elfOH on line 55 and defined in /in/elfOH:43 Stack trace: #0 /in/elfOH(55): Optionz->setInception(NULL) #1 {main} thrown in /in/elfOH on line 43
Process exited with code 255.
Output for 5.5.0 - 5.5.38, 5.6.0 - 5.6.38
it is known this works Catchable fatal error: Argument 1 passed to Optionz::setInception() must be an instance of Optionz, null given, called in /in/elfOH on line 55 and defined in /in/elfOH on line 43
Process exited with code 255.

preferences:
247.07 ms | 402 KiB | 294 Q