Home > Backend Development > PHP7 > body text

New features in PHP7.2 (parameter type declaration)

藏色散人
Release: 2023-02-17 13:06:01
forward
3043 people have browsed it

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);
Copy after login

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
Copy after login

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);
Copy after login

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;
Copy after login

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
Copy after login

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) { /* ... */ }
}
Copy after login

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
Copy after login

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!

Related labels:
source:segmentfault.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template