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.

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.

Define returning type

PHP

1

2

3

4

<?php

functionmultiply(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.

Null coalescend operator

PHP

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().

Constant arrays

PHP

1

2

3

4

5

6

7

8

9

<?php

define('CARS',[

'BMW',

'AUDI',

'TOYOTA'

]);

echoCARS[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.

PHP

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

<?php

// Pre PHP 7 code

usesome\namespace\ClassA;

usesome\namespace\ClassB;

usesome\namespace\ClassC asC;

usefunctionsome\namespace\fn_a;

usefunctionsome\namespace\fn_b;

usefunctionsome\namespace\fn_c;

useconstsome\namespace\ConstA;

useconstsome\namespace\ConstB;

useconstsome\namespace\ConstC;

// PHP 7+ code

usesome\namespace\{ClassA,ClassB,ClassC asC};

usefunctionsome\namespace\{fn_a,fn_b,fn_c};

useconstsome\namespace\{ConstA,ConstB,ConstC};

?>

Integer division with intdiv()

The new intdiv() function performs an integer division of its operands and returns it.

intdiv function

PHP

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.

Anonymous classes

PHP

1

2

3

4

5

6

7

<?php

$object=newclass('parameter'){

public$property;

publicfunction__construct($parameter){

$this->property=$parameter;

}

}

http://code-epicenter.com/new-features-in-php-7-you-should-be-aware-of/New features in PHP 7 you should be aware ofhttp://code-epicenter.com/wp-content/uploads/2016/01/php72.jpghttp://code-epicenter.com/wp-content/uploads/2016/01/php72-150x150.jpg2016-01-11T20:43:32+00:00Amir DuranPHPProgrammingPHP,PHP 7In 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...Amir DuranAmirDuranamir.duran@gmail.comAdministratorAmir Duran is software engineer who currently lives and works in Germany. He obtained Masters degree diploma on Faculty of Electrical Engineering in Sarajevo, department Computer science. With good educational background he is specialized in designing and implementing a full-stack web based applications.Code Epicenter

Related Posts

About Amir Duran

Amir Duran is software engineer who currently lives and works in Germany. He obtained Masters degree diploma on Faculty of Electrical Engineering in Sarajevo, department Computer science. With good educational background he is specialized in designing and implementing a full-stack web based applications.