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);
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.
Recommended related articles:
PHP5.5 ~ PHP7.2 New Features Collection - Personal Article
PHP7.2 Version Performance Introduction
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!