I can't comment yet, but this is too important to ignore; the accepted answer contains a syntax error (as do many other examples on the internet). PHP attributes go before the function declaration, not after. When solving this problem for code that has to be backwards compatible (e.g. wordpress plugins as wordpress still supports 5.x), the correct syntax is this :
#[\ReturnTypeWillChange]
class Example implements Countable {
public function count() {
$this->recalculate();
return $this->amount;
}
}
Otherwise, you'll see an unexpected $this
syntax error.