PHP 7 available new features and improvements

 

PHP 7 available new features and improvements helps the programmer to minify the process.
PHP 7 is the most excited and is a coolest and minifier feature release of PHP scriptong language.
PHP 7 was released on the Dec 2015.
we (php programmer ) celebrate for finally release PHP 7 with great new features and alot of improvements compare than the PHP 5 . We are going to describe below about php 7 features and improvements.

 

PHP 7 available new features and improvements

 

SCALAR TYPE HINTS :

There are some scalar types as defined below :

Integers
Floating point numbers
Booleans
Strings

so , we can used this scalar types as type hints.

 

<?php
/* Scalar type declarations in php 7  */


//declare(strict_types=1);




function addToNumber(int $x, int $y) {




return $x + $y;




}




var_dump(addToNumber(4,5));





var_dump(addToNumber("4","5"));




?>

 

In php 7 ‘Coercive Mode’ is enable By default.

 

Return type declarations :

Before the starting curly brackets of given function , we use colon to hint the return type.

 

<?php

/* Return type declaration */







//declare(strict_types=1);







function addNumber(int $x, int $y): int{





return (string)($x + $y);




}






var_dump(addNumber(4,5));

 

if strict mode is enable , It could not be converted to the preferred type are allowed . It is allowed preferred type when strict mode is disabled.

 

 

Anonymous classes :

Anonymous classes are very useful when we define a class and instantiate an object inline .

 

<?php

/* Anonymous class */



$obj = new class {




public function foo() {




return "hello";





}




};


var_dump($obj,$obj->foo());

 

Closure::call() method :

call() methods is one of the coolest features of PHP 7 which come to minify the process.

 

<?php

/* defined the Closure::call()*/


class Go
{
    private $foo = 'apple';
}




$getGooCallback = function() {



    return $this->foo;
};





//Define in PHP5


$bindingValue = $getGooCallback->bindTo(new Go,'Go');



echo $bindingValue().PHP_EOL;


// Define PHP7


echo $bindingValue->call(new Go).PHP_EOL;

 

Null coalesce operator :

This is one of the coolest features of PHP . Because, It saves alot of time for Deloper. It tells the compiler , if variable is not defined then returns the default value which is set by programmer.
It is denoted by symbol “??”.

Example :

<?php


/* Null coalesce operator*/

$array = ['Exam'=>'pass'];


//Define null in PHP5 


$checkNull = isset($array['Exam']) ? $array['Examp'] : 'fail';


echo $checkNull;



//Define null in PHP7

$checkNull = $array['Exam'] ?? 'fail';


echo $checkNull;


 

Space ship operator :

It is types of operator that makes very easier whien you compare between two ghiven values. The space ship operator returns value based on the folowing evaluation.

Example :

return 0 = If both value are equall.
return 1 = If left value are greater than the right value.
return – = If left value are less than the right value.

Leave a Reply

Your email address will not be published. Required fields are marked *