New features in PHP 7 you should be aware of
In this post you can find some of the new features coming with PHP 7. If you are upgrading from PHP 5.x then you can read my post “Things you should know when upgrading to PHP 7“.
Scalar type declarations
In the PHP 7 it is possible to specify function parameters as certain types (type hinting). If the given value is of the incorrect type, then TypeError exception is thrown. The following types can be enforced as parameters :
- string
- int
- float
- bool
To specify type declaration, just write type name before the parameter name something like int $parameterName.
Here are valid types:
Type | Description | Minimum PHP version |
---|---|---|
Class/interface name | The parameter must be an instanceof the given class or interface name. | PHP 5.0.0 |
self | The parameter must be an instanceof the same class as the one the method is defined on. This can only be used on class and instance methods. | PHP 5.0.0 |
array | The parameter must be an array. | PHP 5.1.0 |
callable | The parameter must be a valid callable. | PHP 5.4.0 |
bool | The parameter must be a boolean value. | PHP 7.0.0 |
float | The parameter must be a floating point number. | PHP 7.0.0 |
int | The parameter must be an integer. | PHP 7.0.0 |
string | The parameter must be a string. | PHP 7.0.0 |
Return type declarations
PHP 7 adds support for return type declarations. Similarly to argument type declarations, return type declarations specify the type of the value that will be returned from a function. The same types are available for return type declarations as are available for argument type declarations.
PHP 7 allows you to define return type declarations. Return type declarations are specifying the type of the value that will be returned from a calling function. Types listed in the table above are available for return type.
1 2 3 4 |
<?php function multiply(int $value): int{ return $value*5; } |
Null coalescing operator
The null coalescing operator (??) has been added as syntactic sugar for the common case of needing to use a ternary in conjunction with isset(). It returns its first operand if it exists and is not NULL; otherwise it returns its second operand.
1 2 3 4 |
<?php $username = $_GET['user'] ?? 'nobody'; // This is equivalent to: $username = isset($_GET['user']) ? $_GET['user'] : 'nobody'; |
Spaceship operator
This operator is used for comparing two expressions, and it returns the following values -1, 0, 1. when $a is respectively less than, equal to, or greater than $b.
Constant arrays using define()
One fancy feature added in PHP 7 is ability of defining constant array values using define().
1 2 3 4 5 6 7 8 9 |
<?php define('CARS', [ 'BMW', 'AUDI', 'TOYOTA' ]); echo CARS[1]; // outputs "BMW" ?> |
Group use declarations
Classes, functions and constants being imported from the same namespace can now be grouped together in a single use statement.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php // Pre PHP 7 code use some\namespace\ClassA; use some\namespace\ClassB; use some\namespace\ClassC as C; use function some\namespace\fn_a; use function some\namespace\fn_b; use function some\namespace\fn_c; use const some\namespace\ConstA; use const some\namespace\ConstB; use const some\namespace\ConstC; // PHP 7+ code use some\namespace\{ClassA, ClassB, ClassC as C}; use function some\namespace\{fn_a, fn_b, fn_c}; use const some\namespace\{ConstA, ConstB, ConstC}; ?> |
Integer division with intdiv()
The new intdiv() function performs an integer division of its operands and returns it.
1 2 3 |
<?php var_dump(intdiv(10, 3));//Will print 3 ?> |
Anonymous classes
In PHP 7 you can define anonymous classes by using keyword new class. Anonymous class can be used in place of full class definitions for throwaway objects.
1 2 3 4 5 6 7 |
<?php $object = new class('parameter'){ public $property; public function __construct($parameter){ $this->property = $parameter; } } |
and in this case? php7.
tks.
Thanks for the nicely written out new features of PHP7.
I did notice an error in one of your examples though:
CARS[1] will not output BMW, but rather AUDI.