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.