PHP 7 - Why does a method that returns the primitive String type not generate an error when returning a Boolean value?

Asked

Viewed 147 times

9

PHP 7 - Why a method that returns the primitive type String, does not generate error when returning a value Boolean?

<?php

class Foo
{
    public function bar() : string
    {
        return true;
    }
}

$Foo = new Foo();
echo $Foo->bar();

?>

Output: 1

https://repl.it/repls/StraightBlondTag

  • The return is actually, string(1) "1".

  • I was just going to say that. PHP converts true for string(1) "1"

  • true in PHP returns 1, example echo true; result is 1, the conversion to text gets "1" that’s what he did! Your comment if echo was the quoted value it returns the text different from your question.

  • Just to add to that: PHP is not a strongly typed language. PHP typing is very weak (which can cause more problems than helping in some cases)

  • So logic should generate an error, but PHP converts to String. This is not good because it breaks the sense of declaring the type of return in the methods.

  • Your question is really good @Leandrosciola has to check this aspect with every reason ...

Show 2 more comments

1 answer

11


By default all code in PHP has checking Weak, and to enable put a line at the top of the file including before the namespace that will ensure and check the types rigorously (checking Strong): (declare(strict_types = 1);):

<?php

    declare(strict_types = 1); // habilitando checagem forte

    class Foo
    {
        public function bar(): string
        {
            return true;
        }
    }

    $Foo = new Foo();
    echo ($Foo->bar());


?>

then it generates the erro:

Return value of Foo::bar() must be of the type string, boolean returned

Online Example

References:

  • 1

    What kind of mistake is that? Fatal, Warning, ...

  • 2

    @Guilhermecostamilam he gives a PHP Fatal error Uncaught TypeError: Return value of Foo::bar() must be of the type string, boolean returned

  • 2

    For me this should be default and people would have to configure in php.ini to put strict_types = Off hehehehe

  • 1

    @Guilhermenascimento But they want to promote broken and buggy code, so no can do! xD

  • @Guilhermenascimento agree, but this would generate a mountain of core bugs. See, for example, that bug I reported three years ago. A dev did not want to accept that it was a core bug and wanted to switch to a documentation problem only because it would "cause too many problems". It ends up "delaying progress".

  • @Gabrielheming is not BUG, yes I understand the side effects, even more so in software written in PHP5, but that’s why I suggested to have the flag to turn off. Besides, I think there were problems in the range of PHP7.0.x from which they themselves issue notes, precisely because this case of yours may not have been the only one. No 7.2 do not know how this the specific case. Anyway thanks for sharing.

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.