On Wed, Feb 25, 2015 at 3:15 PM, Niktia Nefedov <inefe...@gmail.com> wrote:

>  On Wed, 25 Feb 2015 16:55:57 +0400, Dmitry Stogov <dmi...@zend.com>
> wrote:
>
>
>
> On Wed, Feb 25, 2015 at 2:42 PM, Niktia Nefedov <inefe...@gmail.com>
> wrote:
>
>> On Wed, 25 Feb 2015 16:30:32 +0400, Dmitry Stogov <dmi...@zend.com>
>> wrote:
>>
>>  anyone may tell, what this will print without running :)
>>>
>>> main.php
>>> ========
>>> <?php
>>> declare(strict_types=1)
>>> include "a.php";
>>> include "b.php";
>>> var_dump(foo("5"));
>>> ?>
>>>
>>> a.php
>>> =====
>>> <?php
>>> declare(strict_types=0)
>>> function foo(string $a): string {
>>>     bar($a);
>>>     return $a;
>>> }
>>> ?>
>>>
>>> b.php
>>> =====
>>> <?php
>>> declare(strict_types=1)
>>> function bar(int &$a) {
>>>     var_dump($a);
>>> }
>>> ?>
>>>
>>> Thank. Dmitry.
>>>
>>
>>
>> Hi Dmitry,
>>
>> This will error out because $a in the scope of `foo` will be coerced to
>> int type when passed to bar by reference.
>>
>
> I think it'll work without errors and produce some explainable but weird
> output. I didn't run it myself yet.
>
>
> Yep, you're right. But it's not a problem of mixed-mode, it's more of a
> problem of introducing coercive type hints at all, honestly... I wonder
> what Zeev plans to do with __toString coercion in his RFC, because that is
> even more severe problem when your object unnoticedly becomes a string.
>

The object on the call-site should remain to be an object (if it's not
passed by reference), however the called function will receive a string.
It works in PHP-5 and PHP-7. Nothing should be changed.

$ sapi/cli/php -r 'class X {function __toString(){return "abc";}} $x=new X;
var_dump(strlen($x)); var_dump($x);'
int(3)
object(X)#1 (0) {
}

However, declare(strict_types=1) will break this. see
https://github.com/ircmaxell/php-src/compare/scalar_type_hints_v5#diff-ef5bf53d1412b50f85d125ca4fe84741R1182

Thanks. Dmitry.

Reply via email to