I wanted to try out PHP 7's return type declarations (I am using PHP 7RC3 on windows for this purpose).
wanted to start with something very simple:
function getme() : integer
{
return 434;
}
echo getme();
but this gives me an fatal error:
Fatal error: Uncaught TypeError: Return value of getme() must be an instance of integer, integer returned
then i also tried to cast the return value, but
return (integer) 434;
or return (int) 434;
gives me the same error;
finally i also tried:
function getme() : integer
{
$i = 434;
return (integer) $i;
}
echo getme();
with the same result.
what am I doing wrong?
or what have I misunderstood here?
thanks for any explanations and help!
UPDATE
this is why I thought I had to use integer
instead of int
(special note to Toby Allen):
from https://wiki.php.net/rfc/return_types:
Examples of Invalid Use (...)
// Int is not a valid type declaration
function answer(): int {
return 42;
}
answer();
No new reserved words are added. The names int, float, string and bool are recognised and allowed as type declarations, and prohibited from use as class/interface/trait names (including with use and class_alias).
From: https://wiki.php.net/rfc/scalar_type_hints_v5
TL/DR:
function getme() : int
{
return 434;
}
echo getme();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With