[php-src] PR #25497: Deprecate implicit conversion of objects to string for typed properties
| From: | nicolas-grekas | Date: | Mon, 16 Nov 2026 17:48:57 +0000 |
| Subject: | [php-src] PR #25497: Deprecate implicit conversion of objects to string for typed properties | ||
| Groups: | php.git-pulls | ||
| Request: | Send a blank email to git-pulls+get-40702@lists.php.net to get a copy of this message | ||
Pull Request: https://github.com/php/php-src/pull/25497
Author: nicolas-grekas
This PR deprecates assigning an object to a
string typed property in coercive typing
mode. In 9.0, this would throw a TypeError, as it already does with strict_types=1.
The reason is security hardening. When a class has no __unserialize() method,
unserialize() writes the properties itself and rejects an object in a
string property. Adding an __unserialize() method that copies its data to
the same properties silently turns this TypeError into a call to __toString():
```php
class Foo {
public string $name;
// Without this method, unserialize() throws a TypeError for an object in $name.
// With it, the assignment calls __toString() on any class the payload picked.
public function __unserialize(array $data): void {
$this->name = $data['name'];
}
}
```
This is a common gadget chain. The string type looks like validation, but in coercive
mode it runs code that the payload chose. It's the only weak coercion that can call userland
code. It's also behind memory bugs like GH-10169 and GH-14969.
I left parameters out on purpose. Passing a Stringable to a string parameter is a
common idiom.
The deprecation is raised before __toString() is called, so an error handler that
throws prevents the call. The fix is an explicit (string) cast.
Before writing an RFC, I'd like to know if there's interest in this. WDYT?