3v4l.org

run code in 300+ PHP versions simultaneously
<?PHP class Question { public function __construct(AnswerInterface $answer) { echo "What is 2 + 2?\n"; $answer = $answer->get()->the()->answer(); if ($answer instanceof AnswerInterface) { echo $answer . PHP_EOL; } } } interface AnswerInterface { public function get(); public function the(); public function answer(); } class Answer implements AnswerInterface{ private $answer; public function get(){ $this->answer = 2; return $this; } public function the(){ $this->answer = $this->answer + 2; return $this; } public function answer(){ return $this->answer; } } $answer = new Answer; $question = new Question($answer);
Output for 7.3.32 - 7.3.33, 8.0.0 - 8.0.30, 8.1.0 - 8.1.31, 8.2.0 - 8.2.26, 8.3.0 - 8.3.14, 8.4.1 - 8.4.2
What is 2 + 2?
Output for 7.0.0 - 7.0.20, 7.1.0 - 7.1.20, 7.2.6 - 7.2.33, 7.3.16 - 7.3.31, 7.4.0 - 7.4.33
Deprecated: Methods with the same name as their class will not be constructors in a future version of PHP; Answer has a deprecated constructor in /in/g9ekn on line 22 What is 2 + 2?

preferences:
72.01 ms | 408 KiB | 5 Q