上級 › __toString未実装のオブジェクトを文字列化した原因 › パターン5
__toString未実装のオブジェクトを文字列化した原因
__toString()メソッドを持たないオブジェクトを文字列と連結したり、文字列として出力しようとしたりすると致命的エラーになります。
1 <?php 2 class Speed { 3 public function __construct(public int $kmh) {} 4 } 5 $s = new Speed(80); 6 echo "Speed: " . ________________; ^
Fatal error: Uncaught Error: Object of class Speed could not be converted to string in main.php:6
空欄を埋めて実行を通す
Speedに__toString()が無いため、オブジェクトを直接文字列と連結できません。
次の問題