PHP 7.2 New features: Parameter type declaration (with code)

Source: Internet
Author: User
Tags commas in list
The php7.2 version has new features, features and improvements that allow us to write better code, and in the following article I will describe a new feature in php7.2: parameter type declaration, say no more, let's take a concrete look at the text content.

Parameter type declaration

Starting with PHP 5, we can specify the type of parameter expected to be passed in the declaration of the function. If the type of the given value is incorrect, then PHP throws an error. A parameter type declaration (also known as a type hint) specifies the type of variable that is expected to be passed to a function or class method.

Here's an example:

Class MyClass {public    $var = ' Hello world ';} $myclass = new myclass;function Test (MyClass $myclass) {    return $myclass->var;} echo Test ($myclass);

In this code, the test function requires an instance of MyClass. An incorrect data type can cause the following fatal error:

Fatal error:uncaught typeerror:argument 1 passed to test () must is an instance of MyClass, string given, called in/app/ index.php on line and defined In/app/index.php:8

Because PHP 7.2 Type hints can be used with object data types, this improvement allows a generic object to be declared as a parameter to a function or method. Here is an example:

Class MyClass {public    $var = ';} Class FirstChild extends MyClass {public    $var = ' My name is Jim ';} Class Secondchild extends MyClass {public    $var = ' My name is John ';} $firstchild = new FirstChild; $secondchild = new secondchild;function Test (object $arg) {    return $arg->var;} echo Test ($firstchild); Echo test ($secondchild);

In this example, we call two test functions and each call passes a different object. This is not possible in previous versions of PHP.

Object return type declaration

If the parameter type declaration specifies the expected type of the function parameter, the return type declaration specifies the expected type of the return value.

The return type declaration specifies the type of the variable that the function expects to return.

Starting with PHP 7.2, we are allowed to use return type declarations for object data types. Here is an example:

Class MyClass {public    $var = ' Hello world ';} $myclass = new myclass;function Test (MyClass $arg): Object {    return $arg;} echo Test ($myclass)->var;

Previous versions of PHP could cause the following fatal errors:

Fatal error:uncaught Typeerror:return value of test () must is an instance of object, instance of MyClass returned In/ap P/index.php:10

Of course, in PHP 7.2, this code will respond to ' Hello world '.

Parameter type Grace declaration

PHP currently does not allow any differences in the types of parameters between subclasses and their parent classes or interfaces. What does that mean?
Consider the following code:

<?phpclass MyClass {public    function myFunction (array $myarray) {/* ... */}}class Mychildclass extends MyClass { C2/>public function MyFunction ($myarray) {/* ... */}}

Here we omit the parameter types from the subclass. In PHP 7.0, this code generates the following warning:

Warning:declaration of Mychildclass::myfunction ($myarray) should is compatible with myclass::myfunction (array $myarray ) in%s on line 8

Since PHP 7.2, we have been allowed to omit types from subclasses without breaking any code. This recommendation will allow us to upgrade the class to use the type hints in the library without having to update all the subclasses.

Trailing commas in list syntax

The trailing comma after the last item in the

Array is a valid syntax in PHP, and is sometimes encouraged to be used in order to easily attach new items and avoid parsing errors due to missing commas. Since PHP 7.2, we have been allowed to use trailing commas in the grouping namespace.

Related Article

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.