5K Views

New Features In PHP 7

The features of PHP7 are fairly enough for advance web application development and making use of latest resources to boost you development.

PHP 7 was released in December 2015. The main reason behind the PHP7 is PHP6 has never reached to stable version, So PHP community has chosen PHP5.6 to directly PHP7.

 

                                             Necessity To Upgrade:

 

Features:

Group use declaration:

use FooLibrary\Bar\Baz\ClassA;
use FooLibrary\Bar\Baz\ClassB;
use FooLibrary\Bar\Baz\ClassC;
use FooLibrary\Bar\Baz\ClassD as Fizbo;

// In PHP 7 : 
use FooLibrary\Bar\Baz\{ ClassA, ClassB, ClassC, ClassD as Fizbo};

 

// Current use syntax:
 
use Symfony\Component\Console\Helper\Table;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Question\Question;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\Console\Question\ChoiceQuestion as Choice;
use Symfony\Component\Console\Question\ConfirmationQuestion;
 
// Proposed group use in PHP7 syntax:
 
use Symfony\Component\Console\{
  Helper\Table,
  Input\ArrayInput,
  Input\InputInterface,
  Output\NullOutput,
  Output\OutputInterface,
  Question\Question,
  Question\ChoiceQuestion as Choice,
  Question\ConfirmationQuestion,
};

Return type declaration:

Return type declaration specifies that the type of value that a function should return.

//declare(strict_types = 1);
function returnIntValue(int  $value) :  int {
     return $value; 
}
print(returnIntValue(5));

 

Scalar type hints:

Type hints have been available in PHP for while now. Unfortunately they were restricted to classes, arrays and callables.
As of PHP 7, the scalar types (integers, floating point numbers, Booleans and strings) can also be used as type hints.

//declare (strict types=1)
function add(int $a  int $b) {
	   return $a + $b;
}
var_dump(add(1,2));

 

Level support for dirname() function:

In PHP 4 and higher:
return dirname ( dirname (_file_));

In PHP 7:
return dirname (_file_ , 2);

echo dirname("c:/testweb/home.php") . "<br />";
echo dirname("c:/testweb/home.php", 2) . "<br />";

o/p:

c:/testweb
c:/

 

The null coalesce operator:

We have used Ternary operator in PHP 5.

$id = isset ($_GET[‘id’]) ? $_GET[‘id’] : null

Now php7 provided null coalescing operator (??) to accomplish it easy way.

$id = $_GET['id'] ?? null;

Coalescing can be chained:

$id = $_GET['id'] ?? $_POST['id'] ?? null;

 

Spaceship operator (<=>)

Spaceship operator return only -1, 0 or 1 by performed to compare two values.

echo $a <=> $b;  
echo $a <=> $b;  
echo $a <=> $b;

$a > $b then it will return 1.
$a < $b then it will return -1.
$a = $b then it will return 0.

 

Generator delegation:

function gen()
{
yield 1;
yield 2;
yield from gen2();
}

function gen2()
{
yield 3;
yield 4;
}

foreach (gen() as $val)
{
echo $val . ‘&nbsp’;
}

O/P : 1 2 3 4

 

I’ve explored this while contributing to Symfony based project UVdesk, there are a lot more things to learn and you could also contribute to an enterprise-level open source helpdesk.

Thanks for reading me. Hope it will help someone to use new features of PHP7.

 

UVdesk Forum!          Developer Visit!             Contact Us!         Live Demo!

 

Category(s) PHP7 Symfony UVdesk
. . .

Comment

Add Your Comment

Be the first to comment.

css.php