strict_types Exceptions

<?php declare(strict_types = 1);

class x {
  function __toString() : string {
    return "abc";
  }
}

$x = new x;
print $x . " and $x
";

print $x;
echo $x;
echo PHP_EOL;

print implode(', ', [$x, $x]);
print implode($x, [1, 2]);

?>

strict_types do not apply to PHP operators, only on to typed structures.

Here, concatenation and interpolation all call __toString(), but not foo().

As you can see, print() and echo() are safe too, while implode() is not: actually, it is not safe for the first argument, but still OK with the elements of the array, in the second argument.

See Also

PHP Features

Last updated: 14 July 2026