Это очень старая проблема с тем, что php в случае рекурсии сегфолтится, а не фатал кидает. Но там нет нормального решения без существенного снижения производительности.
count(new class implements \Countable {
function count() {
return count($this);
}
});
PHP:
(new class{
function __call($a,$b){
return $this->foo();
}
})->foo();
PHP:
(new class{
public function __get($x){
$a = clone $this;
return $a->$x;
}
})->foo;
PHP:
serialize(new class implements \Serializable{
public function serialize() {
return serialize(clone $this);
}
public function unserialize($data) {}
});
PHP:
$a = new class implements \ArrayAccess {
public function offsetSet($offset, $value) {}
public function offsetExists($offset) {}
public function offsetUnset($offset) {}
public function offsetGet($offset) {
return $this[$offset];
}
};
echo $a['foo'];