<?php
class StringCastable
{
private $data;
public function __constructor($data)
{
$this->data = $data;
}
public function __toString()
{
return $this->toString();
}
public function toString()
{
return $this->data;
}
}
function testStringCast(string $data)
{
var_dump($data, get_class($data));
}
testStringCast(new StringCastable("speculoos"));
Fatal error: Uncaught TypeError: StringCastable::__toString(): Return value must be of type string, null returned in /in/GI3Y7:14
Stack trace:
#0 /in/GI3Y7(23): StringCastable->__toString()
#1 /in/GI3Y7(28): testStringCast(Object(StringCastable))
#2 {main}
thrown in /in/GI3Y7 on line 14
Process exited with code 255.
Output for 7.4.0 - 7.4.33
Fatal error: Uncaught Error: Method StringCastable::__toString() must return a string value in /in/GI3Y7:23
Stack trace:
#0 /in/GI3Y7(28): testStringCast(Object(StringCastable))
#1 {main}
thrown in /in/GI3Y7 on line 23
Process exited with code 255.
Catchable fatal error: Argument 1 passed to testStringCast() must be an instance of string, instance of StringCastable given, called in /in/GI3Y7 on line 28 and defined in /in/GI3Y7 on line 23
Process exited with code 255.