20
Still in PHP 5 it was already possible to make the declaration of types in arguments of function.
Type statements
Type declarations allow functions to require parameters to be of certain types when calling them. If the value entered in the parameter has an incorrect type then an error is generated: in PHP 5 it will be a fatal error recoverable, while in PHP 7 will throw a Typeerror exception
To declare the type your name must be added before in the name of parameter. The declaration can be made to accept NULL if the value default parameter is also set to NULL.
From PHP 7 it is possible to declare type of return of a particular function.
Declaration of type of return
PHP 7 adds support for return type declaration. Similar to statement of argument typing, return type declaration specifies the type of the value that will be returned from a function. The same types that are available for argument statement are available for back typing.
Strict typing also affects return typing. In standard mode (weak casing) the returned values will be converted to the type correct if they do not fit the type reported. In strong typing mode the returned values must be the correct type or an exception Typeerror will be launched.
However, in the documentation, nothing is said about the need for the type to be declared or not in the current scope. I did the test and I realized that PHP does not check if the type is declared and I still try the same behavior above.
function foo(): Foo {
return 'foo';
}
foo();
The exit will be the exception TypeError
:
Return value of foo() must be an instance of Foo, string returned
How, then, the language checks whether the return is an instance of Foo
without the class Foo
is there? Evaluating the behavior of the structure instanceof
we can realize that it accepts verification from only the class name, as string.
$obj = 'foo';
$class = 'Foo';
var_dump($obj instanceof $class); // bool(false)
Is this the behavior used in checking argument types and returns? The interpreter stores internally the type as string and checks only from the name?
Very good question, also would like to know this in more detail.
– Filipe L. Constante
This is a very interesting question. Could you please publish the question at https://bugs.php.net/report.php and give a more detailed assessment of the case?
– GustavoAdolfo
Anderson, I think the assessment is wrong. Your
var_dump
demonstrates that$obj
is not instance of Foo. Similarly, theTypeError
launched is correct, since the initial validation (string !== (instance of Foo)) occurs, it is not necessary for the interpreter to check whether the Foo class exists or not. The class existence error would occur if the return isreturn new Foo
.– Rafael Araújo
@Rafaelaraújo But that’s exactly the question, about how PHP analyzes the type of return even when the type is not defined. I didn’t understand what the wrong assessment would be.
– Woss
@Andersoncarloswoss the first check is
retornoDaFuncao === retornoDefinidoDaFuncao
. This verification is done via the metada of the data type name and therefore it is not necessary that the data type actually exists.– Rafael Araújo
@Rafaelaraújo This would not be the answer? What is the wrong assessment you say I asked in the question? The
var_dump
at the end was to show that theinstaceof
works with strings and there doesn’t have to be the type to do that check. Soon after that I asked if this is how the interpreter does internally, if it stores the type of return as string to make the check later. If you know how it works internally, please prepare a reply :D– Woss
All right. I’m gonna throw it in response. ;-)
– Rafael Araújo
Related: https://answall.com/q/93707/101
– Maniero