3v4l.org

run code in 300+ PHP versions simultaneously
<?php class Model { protected $comments; public function __construct() { $this->comments = new class { public function count() { $responses = [ 1, 2, 3, "Error: could not connect to database", 5 ]; $rand = rand(0, 4); if (is_string($responses[$rand])) { throw new Exception($responses[$rand]); } return $responses[$rand]; } }; } } class Post extends Model { public function commentCount() { static $cache; return $cache?: $cache = $this->comments->count(); } } $post = new Post(); var_dump($post->commentCount()); var_dump($post->commentCount()); var_dump($post->commentCount()); var_dump($post->commentCount()); var_dump($post->commentCount());

preferences:
37.6 ms | 402 KiB | 5 Q