website/errors/encapsedStringPart.nonString.md
<?php declare(strict_types = 1);
function greet(\stdClass $obj): string
{
return "Hello, $obj!";
}
An interpolated (double-quoted) string contains an expression that cannot be cast to string. In PHP, when a variable is embedded inside a double-quoted string, PHP attempts to convert it to a string. Objects that do not implement the __toString() method cannot be converted, resulting in a fatal error at runtime.
Access a string property of the object instead of interpolating the object directly:
<?php declare(strict_types = 1);
function greet(\stdClass $obj): string
{
- return "Hello, $obj!";
+ return "Hello, {$obj->name}!";
}
Or implement the __toString() method on the class:
<?php declare(strict_types = 1);
-function greet(\stdClass $obj): string
+class User
+{
+ public function __construct(public string $name) {}
+
+ public function __toString(): string
+ {
+ return $this->name;
+ }
+}
+
+function greet(User $user): string
{
- return "Hello, $obj!";
+ return "Hello, $user!";
}