Home  >  Article  >  Backend Development  >  New features in PHP7.2 (parameter type declaration)

New features in PHP7.2 (parameter type declaration)

藏色散人
藏色散人forward
2019-11-11 13:51:263047browse

PHP 7.2 has been officially released with new features, functionality and improvements that allow us to write better code. In this article, I'll introduce some of the most interesting language features in PHP 7.2 - parameter type declarations.

Recommended: "PHP7 Tutorial"

Parameter type declaration

Starting from PHP 5, we can declare the function in Specifies the type of parameters expected to be passed. If the given value is of incorrect type, then PHP will throw an error. A parameter type declaration (also called a type hint) specifies the type of variables expected to be passed to a function or class method.

Let’s take 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. Incorrect data types result in the following fatal error:

Fatal error: Uncaught TypeError: Argument 1 passed to test() must be an instance of MyClass, string given, called in /app/index.php on line 12 and defined in /app/index.php:8

Since PHP 7.2 type hints can be used with object data types, and this improvement allows generic objects to be declared as parameters of functions or methods. 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 the test function twice, passing a different object in each call. This was not possible in previous PHP versions.

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 variable expected to be returned by the function.

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 PHP versions would result in the following fatal error:

Fatal error: Uncaught TypeError: Return value of test() must be an instance of object, instance of MyClass returned in /app/index.php:10

Of course, in PHP 7.2, this code will respond with 'Hello World'.

Parameter Type Grace Statement

PHP currently does not allow any differences in parameter types between subclasses and their parent classes or interfaces. what does that mean?

Consider the following code:

<?php
class MyClass {
    public function myFunction(array $myarray) { /* ... */ }
}
class MyChildClass extends MyClass {
    public function myFunction($myarray) { /* ... */ }
}

Here we have omitted the parameter type in the subclass. In PHP 7.0, this code produces the following warning:

Warning: Declaration of MyChildClass::myFunction($myarray) should be compatible with MyClass::myFunction(array $myarray) in %s on line 8

Since PHP 7.2 we are allowed to omit types in subclasses without breaking any code. This suggestion will allow us to upgrade classes to use type hints in the library without having to update all subclasses.

Trailing comma in list syntax

Trailing comma after the last item in an array is valid syntax in PHP, sometimes to easily append new items and avoid errors due to missing comma causes parsing errors, its use is encouraged. Since PHP 7.2, we are allowed to use trailing commas in grouping namespaces.

See Trailing Commas in List Syntax for a more in-depth look at this RFC and some code examples.

The above is the detailed content of New features in PHP7.2 (parameter type declaration). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:segmentfault.com. If there is any infringement, please contact admin@php.cn delete