MIR1207
InvalidToString
An object is used where a string is expected (e.g., string concatenation, echo, or string
interpolation), but the object’s class does not implement the __toString method. This produces
a fatal error in PHP.
Example
Section titled “Example”<?phpclass Point { public function __construct( public float $x, public float $y, ) {}}
$p = new Point(1.0, 2.0);echo "Point: " . $p; // Point has no __toString methodHow to fix
Section titled “How to fix”Add a __toString method to the class, or explicitly convert the object to a string before
use:
<?phpclass Point { public function __construct( public float $x, public float $y, ) {}
public function __toString(): string { return "({$this->x}, {$this->y})"; }}