A detailed description of the function type declarations for each version of PHP

Source: Internet
Author: User
This article mainly summarizes 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, hope to help everyone.

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:

    1. Class/interface

    2. Self, it can only be used in its own way.

    3. Array

    4. bool

    5. Callable

    6. Int

    7. Float

    8. String

    9. 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.

Related recommendations:

How to use type constraints to qualify PHP function types

JavaScript function Type description

Javascript Judgment function type Perfect solution _javascript tips

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.