Menu

← Back to encapsedStringPart.*

Error Identifier: encapsedStringPart.nonString

Every error reported by PHPStan has an error identifier. Here’s a list of all error identifiers. In PHPStan Pro you can see the error identifier next to each error and filter errors by their identifiers.

Code example #

<?php declare(strict_types = 1);

function greet(\stdClass $obj): string
{
    return "Hello, $obj!";
}

Why is it reported? #

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.

How to fix it #

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!";
 }

How to ignore this error #

You can use the identifier encapsedStringPart.nonString to ignore this error using a comment:

// @phpstan-ignore encapsedStringPart.nonString
codeThatProducesTheError();

You can also use only the identifier key to ignore all errors of the same type in your configuration file in the ignoreErrors parameter:

parameters:
	ignoreErrors:
		-
			identifier: encapsedStringPart.nonString

Rules that report this error #

  • PHPStan\Rules\Cast\InvalidPartOfEncapsedStringRule [1]

Edit this page on GitHub

Theme
A
© 2026 PHPStan s.r.o.