PHP7 has been released as early as 2015, and more and more people are using it. In this article, we are going to talk about how PHP7 declares scalar types and how PHP7 performs strong type verification. Students who don’t understand can Take a look!
Scalar type declaration
PHP has supported class and interface parameter type declaration since PHP5.0, PHP5.1 Supports array and PHP5.4 supports callable. These type declarations allow PHP to pass in the correct parameters during execution, making the function signature more informative.
Starting from PHP7, int, float, string and bool will be recognized as type declarations and can be used for function return value types and parameter type declarations
<?php function sum(int $a,int $b):string { return $a+$b; } var_dump(sum(1,2));
Running the above code is normal
The result is string(1) "3", in weak type checking mode, if the parameter does not meet the declared type, it will be converted according to PHP rules
PHP7.1 adds a declaration method for nullable types and adds void As a function return value type, a function whose return type is defined as void in strong type checking mode cannot have a return value, even if it returns null:
declare(strict_types=1); function returns_one(): void { return 1; // Fatal error: A void function must not return a value } function returns_null(): void { return null; // Fatal error: A void function must not return a value }
In addition, void only applies to return types and cannot be used Parameter type declaration, otherwise an error will be triggered:
function foobar(void $foo) { // Fatal error: void cannot be used as a parameter type }
Nullable type declaration
Nullable type is mainly used for parameter type declaration and function return value declaration.
The main two forms are as follows:
function answer(): ?int{ return null; //ok }function answer(): ?int{ return 42; // ok } function say(?string $msg) { if ($msg) { echo $msg; } }
It is easy to understand from the example. It refers to the form of ? to indicate that the type of function parametersor return value is either Specifies the type, or null.
This method can also be used for the definition of interface functions:
interface Fooable { function foo(?Fooable $f); }
But there is one thing to note: if the function itself defines the parameter type and has no default value, even if it is nullable, It cannot be omitted, otherwise an error will be triggered.
is as follows:
function foo_nullable(?Bar $bar) {}foo_nullable(new Bar); // 可行 foo_nullable(null); // 可行 foo_nullable(); // 不可行
But if the parameters of the above function are defined in the form of ?Bar $bar = null, the third way of writing is also feasible. Because = null is actually equivalent to a superset of ?, null can be set as the default value for parameters of nullable types.
The declaration of return type in a class function cannot be overridden by a subclass, otherwise an error will be triggered:
class Foo { public function bar(): void { } }class Foobar extends Foo { public function bar(): array { // Fatal error: Declaration of Foobar::bar() must be compatible with Foo::bar(): void } }
Strict verification mode
strict_types/declare() directive
By default, all PHP files are in weak type checking mode. The new declare directive specifies the value of strict_types (1 or 0). 1 indicates strict type checking mode, which applies to function calls and return statements; 0 indicates weak type checking mode.
declare(strict_types=1) must be the first statement of the file. If this statement appears elsewhere in the file, a compilation error will be generated, and block mode is explicitly prohibited.
Similar to the encoding directive, but different from the ticks directive, the strict_types directive only affects the specified file and will not affect other files included by it (through include, etc.). This directive is compiled at runtime and cannot be modified. The way it works is to set a flag in the opcode to make function calls and return type checks comply with type constraints.
<?php declare(strict_types=1); function sum(int $a,int $b):string { return $a+$b; } var_dump(sum(1,2));
The above code does not conform to the function’s return value type declaration, and a TypeError will be thrown
<?php declare(strict_types=1); function sum(string $a,string $b):string { return $a+$b; } var_dump(sum(1,2));
The above code does not conform to the function’s parameter type declaration, and will also throw An error occurred
One exception is that the widetype conversionallows int to become float. That is to say, if the parameter is declared as float type, it can still accept int parameters.
<?php declare(strict_types=1); function sum(int $a,int $b):float { return $a+$b; } var_dump(sum(1,2));
The above code runs normally
In this scenario, we pass an int parameter to the function defined to accept float, and this parameter will be converted to float. Conversions other than this are not allowed.
Related recommendations:
PHP7.2 version performance introduction
The above is the detailed content of PHP7 declares scalar types and explains strong type checking. For more information, please follow other related articles on the PHP Chinese website!