Currently in RC3, PHP 7.2 is planned to be released on November 30. The new release is coming with new features, functions, and improvements that will allow us to write better code. In this post, I will introduce some of the most interesting language features coming with PHP 7.2. You can see the full list of upcoming changes on the Requests For Comments page. Core Improvements Argument type declarations Since PHP 5 we are allowed to specify in a function’s declaration the argument type that is expected to be passed. If the given value is of an incorrect type, then PHP throws an error. Argument type declarations (also known as type hints) specify the type of a variable that is expected to be passed to a function or class method. Here is 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 expects an instanceof MyClass. An incorrect data type would 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
Share This