This article gives you a summary of the following PHP version of the function of the type declaration of the use of the method, very simple and practical, the need for small partners can refer to the following
PHP7 began to support scalar type declarations, and strong-type languages were more dense. Use this feature to step over two pits: one is to declare a Boolean, and the most recent is to declare a double. To avoid continuing to make similar mistakes in the future, the official documents were doubled. This article is a summary of the use of the PHP function's type declaration after reading it.
Syntactically, the function definition of PHP has been through several periods:
Ancient times (PHP 4)
Defining a function is very simple, using the syntax declaration of function name (args) {body}. You cannot specify parameters and return value types, and there is an infinite number of possible parameters and return value types. This is by far the most common way to declare functions.
Array and reference type parameter value declaration (PHP 5)
arrays, classes (class), interfaces (interface), functions (callable) can be used in function declarations. Starting with 5.6, constants are supported (including class constants) as default parameters, as well as parameter arrays (with ellipses ...). As a prefix). For example:
function sum (... $numbers) { $sum = 0; foreach ($numbers as $number) { $sum + = $number; } return $sum;}
Note: If the value of the parameter may be null,null, it must be the default value of the parameter, otherwise an error is called. For example:
function foo (array $arr = null) { ...}
Scalar type and return value declaration (PHP 7)
The function formally supports a scalar type (int, bool, float, etc.) and a return value type (which can be declared as a type with parameters). Starting with this version, writing PHP has a feeling like writing java.
Unfortunately, you cannot specify a return value type if the function return value is likely to be null. For example:
function Getmodel (): Foo { if ($this->_model = = = null) { $this->_model = xxxx;//get from DB or otherelse< c4/>} return $this->_model; If $this->_model is still null, run an error}
Parameters and return values can be null and void return type declarations (PHP 7.1)
When the parameter and the return value type are likely to be null, the type is decorated with a question mark (?), which resolves the null value problem (which does not conflict with the default argument), the type declaration is new iterable, and the void type return value is also supported. For example:
function Getmodel (? int $id):? Foo { if ($id!== null) { $this->_model = xxxx; } else { $this->_model = yyyy; } return $this->_model;} Call $foo->getmodel (null); $foo->getmodel (100); The function declares the parameter and does not provide a default argument, the call does not pass in the argument throws an error//changes the function declaration to Getmodel (? int $id = 100) {}, can not pass parameter $foo->getmodel ();
When the function return value is void, the return of the function body cannot be followed by any type, or the return statement does not appear.
function test (array $arr): void { if (!count ($arr) { return; } Array_walk ($arr, function ($elem) {xxxx});}
After reviewing the above history, we can see that PHP 7.1, the function type declaration is very perfect (although in practice, not many).
Say more about the pits that have been trampled in practice. Parameters and return value type declarations are available in the following types:
Class/interface
Self, it can only be used in its own way.
Array
bool
Callable
Int
Float
String
Iterable
Note The list does not have a Boolean and double type! Unless you define both types, it is wrong to use them in parameters and return values!
This is the place where PHP has a bit of an egg ache. The usual double and float two keywords are almost identical, for example Doubleval is the alias of Floatval, Is_double is the alias of Is_float, the conversion with (double) and (float) effect is the same. However, the type declaration does not work here, and the same situation occurs on bool and Boolean.
Summarize
Currently the PHP 7.2 stable version has been released and it is recommended to use PHP 7.1 and subsequent versions as much as possible in new projects. To write clear and maintainable code, it is recommended to declare the type. It is recommended that reference types or strings use null values, and that parameters of scalar types, such as int/float, do not use NULL as much as possible. FUNC_GET_ARGC functions, such as non-essential, try not to use.
Articles you may be interested in:
PHP Implementation Statistics How many occurrences of a number in a sorted array are explained
PHP calls FFmpeg to video and stitching scripts
A detailed description of the scenario (scenario) and validation rules (rule) in Yii2