search
HomeBackend DevelopmentPHP7New features in PHP7.2 (parameter type declaration)

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. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools