Recently I was checking out on PHP 7, specifically return type declaration and type hinting. I have compiled PHP 7 from source(master branch from Github) and running it in Ubuntu 14.04 virtual box. I tried to run following code to get a test of new Exceptions. But it Gave a blank page.
<?php
function test(): string {
return [];
}
echo test();
Then I realize I have to set error to be displayed on screen. So I added old fashioned ini_set('display_errors', 1);
like below,
<?php
ini_set('display_errors', 1);
function test(): string {
return [];
}
echo test();
that gave me following TypeError
as expected according to this Throwable interface RFC
Fatal error: Uncaught TypeError: Return value of test() must be of the type string, array returned in /usr/share/nginx/html/test.php on line 7 in /usr/share/nginx/html/test.php:7 Stack trace: #0 /usr/share/nginx/html/test.php(10): test() #1 {main} thrown in /usr/share/nginx/html/test.php on line 7
Digging further I added declare(strict_types=1);
at the top as below,
<?php declare(strict_types=1);
ini_set('display_errors', 1);
function test(): string {
return [];
}
echo test();
and bang, error just got disappeared leaving me with blank page. I cant figure out why it is giving me a blank page?
declare(strict_types=1); This means that the strictness of typing for scalars is configured on a per-file basis. This directive not only affects the type declarations of parameters, but also a function's return type.
declare(strict_types=1); The good thing about declaring a PHP file as strict is that it actually applies to ONLY the current file. It ensures that this file has strict types, but it doesn't apply to any other file in the whole project.
The declare keyword sets an execution directive for a block of code. If the declare statement is not followed by a block then the directive applies to the rest of the code in the file. There are three directives which can be declared: ticks , encoding and strict_types .
Strict typing ¶ By default, PHP will coerce values of the wrong type into the expected scalar type declaration if possible. For example, a function that is given an int for a parameter that expects a string will get a variable of type string. It is possible to enable strict mode on a per-file basis.
After searching around the google and RFC's I came to follwing sentence in RFC,
This RFC further proposes the addition of a new optional per-file directive, declare(strict_types=1);, which makes all function calls and return statements within a file have “strict” type-checking for scalar type declarations, including for extension and built-in PHP functions.
This means there was nothing wrong with directive declare(strict_types=1)
but the problem was the way I was calling ini_set()
function. It expects second parameter to be of string
type.
string ini_set ( string $varname , string $newvalue )
I was passing int
instead, and hence the setting needed to display errors itself failed to set and hence I was hit
with a blank page by PHP strict mode. I then changed the code a bit and passed the string "1"
as below and it worked.
<?php declare(strict_types=1);
ini_set('display_errors', "1");
function test(): string {
return [];
}
echo test();
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