PHP 7.2 new feature: parameter type declaration (with code)

不言
Release: 2023-04-03 17:40:02
Original
6073 people have browsed it

The php7.2 version has new features, functions and improvements that allow us to write better code. In the following article, I will introduce a new feature in php7.2: parameter type declaration. Not much to say, Let's take a closer look at the text content.

Parameter type declaration

Starting with PHP 5, we can specify the expected parameter types in the declaration of the function. 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.

Recommended related articles:

PHP5.5 ~ PHP7.2 New Features Collection - Personal Article

PHP7.2 Version Performance Introduction

PHP7.2 New Features

The above is the detailed content of PHP 7.2 new feature: parameter type declaration (with code). For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!