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
The return is actually,
string(1) "1"
.– Woss
I was just going to say that. PHP converts
true
forstring(1) "1"
– Wallace Maxters
true
in PHP returns1
, exampleecho 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.– novic
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)
– Wallace Maxters
Related
– Wallace Maxters
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.
– Renexo
Your question is really good @Leandrosciola has to check this aspect with every reason ...
– novic