RE: [PHP-DEV] PHP7 and types
| From: | François Laupretre | Date: | Mon, 13 Jul 2015 22:56:10 +0000 |
| Subject: | RE: [PHP-DEV] PHP7 and types | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-87160@lists.php.net to get a copy of this message | ||
> De : Ryan Pallas [mailto:derokorian@gmail.com]
>
> With all the new typing and strict mode in PHP7, I'm wondering if there is
> any planned support for typing of properties in a future version. IE
> something like:
>
> class Foo {}
> class Bar {
> public Foo $foo;
>
> public function __construct(Foo $foo) {
> $this->foo = $foo;
> }
> }
>
> This seems really useful to me. If its not something that was considered
> before I can try to write up an RFC for this, however I'm not sure if this
> was already considered and denied. Any feedback would be greatly
> appreciated.
Hi,
I proposed this as an addition to scalar type hinting when we were discussing the STH RFC some
months ago. Zeev and Dmitry, like Rasmus today, thought it was probably not possible to implement it
without huge changes to the engine. After thinking more about it, I think they're absolutely
right. What you want to implement is not an additional compile-time check, it's attaching type
hints to object properties. That's a runtime check *and* a possible conversion *each* time a
property is assigned. The problem is that the current engine makes it impossible to trap every zval
assignments, let alone the question of performance. If you look at parameter type hints, argument
types are checked only once, when they are received by the function. Then, in the function body, you
can assign any value/type to every received argument, that's impossible to check.
Rewriting your example to show the feature has nothing to do with compile-time checks :
public function __construct(Foo $foo) {
manage($foo);
$this->foo=$foo; /* Compile-time OK, Runtime error */
}
/* In another file, somewhere... */
public function manage(&$obj)
{
...
$obj='bar';
}
Regards
François