Le 13/07/2014 17:17, Rowan Collins a écrit :
On 13/07/2014 15:59, Jocelyn Fournier wrote:
From my point of view, if the type annotations are doing implicit cast
(with or without E_NOTICE/E_STRICT warning), they should behave
exactly the same than an explicit cast. If it behaves differently,
I'll be really difficult for a developer to guess what will be the PHP
behaviour with this new syntax.

The problem is, in PHP an explicit type cast never fails - (int)'abc'
simply gives you the value 0, not an error.

Yes, but if you add a E_NOTICE / E_STRICT warning with this cast, you'll be aware something wrong could happen with this code, and you'd better explicit cast your variable.


If you let scalar typehints
just generate unchecked casts, we'd have this:

wants_object(object $foo) { var_dump($foo); }
wants_int(int $foo) { var_dump($foo); }

wants_object('abc'); // fails with E_RECOVERABLE_ERROR
wants_int('abc'); // succeeds and prints int(0)

Actually it would be

wants_object('abc'); // fails with E_RECOVERABLE_ERROR
wants_int('abc'); // succeeds and prints int(0) + E_NOTICE / E_STRICT warning


That seems both inconsistent and less useful than a hybrid juggling +
validation approach.

Than means you find currently inconsistant than

$foo = (int) 'abc';

works but not

$bar = (object) 'abc';

? :)


  Jocelyn

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to