<?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());
Fatal error: Uncaught Exception: Error: could not connect to database in /in/Yfmub:23
Stack trace:
#0 /in/Yfmub(38): class@anonymous->count()
#1 /in/Yfmub(44): Post->commentCount()
#2 {main}
thrown in /in/Yfmub on line 23
Process exited with code 255.
Parse error: syntax error, unexpected 'class' (T_CLASS) in /in/Yfmub on line 9
Process exited with code 255.
Output for 5.3.0 - 5.3.29
Parse error: syntax error, unexpected T_CLASS in /in/Yfmub on line 9
Process exited with code 255.
Output for 5.1.0 - 5.1.6, 5.2.0 - 5.2.17
Parse error: syntax error, unexpected T_CLASS, expecting T_STRING or T_VARIABLE or '$' in /in/Yfmub on line 9
Process exited with code 255.
Output for 5.0.0 - 5.0.5
Parse error: parse error, unexpected T_CLASS, expecting T_STRING or T_VARIABLE or '$' in /in/Yfmub on line 9
Process exited with code 255.
Output for 4.4.2 - 4.4.9
Parse error: syntax error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Yfmub on line 5
Process exited with code 255.
Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or '}' in /in/Yfmub on line 5
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error, expecting `T_OLD_FUNCTION' or `T_FUNCTION' or `T_VAR' or `'}'' in /in/Yfmub on line 5
Process exited with code 255.