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