Skip to content

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.

<?php
class Point {
public function __construct(
public float $x,
public float $y,
) {}
}
$p = new Point(1.0, 2.0);
echo "Point: " . $p; // Point has no __toString method

Add a __toString method to the class, or explicitly convert the object to a string before use:

<?php
class Point {
public function __construct(
public float $x,
public float $y,
) {}
public function __toString(): string {
return "({$this->x}, {$this->y})";
}
}