Jan 12, 2016 06:41 PM
(This post was last modified: Apr 12, 2016 12:50 PM by Sakuto.)
Summary of PHP 7 features
Hello everybody, as you all know, PHP 7 is finally available in every good hosting service, and it's for this reason that we will take a look at the major features of this new version. I will try to explain everything in a clear way with example, and I hope you will enjoy reading it :)
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Scalar type
PHP 7 introduce a whole new feature, as you were already able to do it before with class, you are now able to use scalar type to define the expected type for parameter, but also the return type of a function. Here's a quick example:PHP Code
<?php
function testFunction(float $myNumber) {
echo "Your number is: $myNumber";
}
testFunction(12.5); // will work perfectly
testFunction(12); // with a boxing method, PHP will handle this int passed while a float was expected
You have two level of verification for scalar type, strict mode and non-strict. In the non-strict mode (the default mode), PHP will try to convert the given variable into the right type with a boxing system. Contrariwise, in strict mode, if the given variable doesn't match the type, PHP will return a Fatal Error. For example, if we are taking back our previous code, the second call of our function would make the script return a Fatal Error. You can easily switch from one to the other by typing this in the beginning of your page.
PHP Code
<?php
declare(strict_types=1);
But what would've been scalar type without return type? Nothing really interesting. You are now, in addition, able to define a return type for your function, let's take our previous code again and implement it!
PHP Code
<?php
function testFunction(float $myNumber): string {
return "Your number is: $myNumber";
}
testFunction(12.5); // will work perfectly
testFunction(12); // with a boxing method, PHP will handle this int passed in a float
PHP actually support eight primitive type:
- Four scalar types
- boolean
- integer
- float (or double)
- string
- boolean
- Two compound types
- array
- object
- array
- Two special types
- resource
- NULL
- resource
- But also pseudo-type
- mixed
- number
- callback (or Callable)
- void
- mixed
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Null coalescing Operator
Great addition to this version, the null coalescing Operator, this simple operator will allow you to drasticaly reduce your code! It will return take the first parameter if it work, otherwise the second. Here's an example:PHP Code
<?php
$pageId = isset($_GET['pageId']) ? $_GET['pageId'] : 1; // Before
$pageId = $_GET['pageId'] ?? 1; // After
$pageId = $_GET['pageId'] ?? $_SESSION['lastPage'] ?? 1; // We can chain with this operator
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Tie Fighter Operator
New little feature that will allow you to compare two variables. It will return:- 0: if both variables are identical
- -1: if the first variable is the greatest
- 1: if the second variable is the greatest
PHP Code
<?php
1 <=> 1 // return 0
0 <=> 1 // return -1
1 <=> 0 // return 1
Very simple addition, but could be useful and allow you to avoid multiple condition with if, elseif, else...
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Array constants
You are now able to declare array in constant, nothing really hard to explain here so let me quickly show you an example:PHP Code
<?php
const BESTUSER = ['Sakuto', 'StackOverlowin'];
var_dump(BESTUSER);
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Group use declaration
The new version give us the ability to group multiple use in one instruction, particulary useful when working with Framework or a lot of library.PHP Code
<?php
// Before
use App\HF\Forum;
use App\HF\User;
use App\HF\God as Sakuto;
// Now
use App\HF\{Forum, User, God as Sakuto}
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Closure Call
We're now able to inject closure into existing class with the method ->call(). This method will be executed as a method of this class and so will have access to the context of the current class instance. PHP Code
<?php
class myClass {
public $firstVar;
}
$foo = new myClass();
$foo->firstVar = 'bar';
$showValue = function() { return $this->firstVar; };
echo $showValue->call($foo);
A bit of explanation for this piece of code. We are creating a new class with a public attribute firstVar. Instantiate and assign a value to your instance of class myClass. We are then creating a closure that just return the variable firstVar.
When calling the closure on the foo instance, the current context will be equals to the context, the closure will get access to the attribute firstVar and will correctly return "bar";
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Assert
The assert function is fully changed, in the last version of PHP, we were only able to return a string as the second parameter. In the version 7, you can now return custom class to avoid typing again and again the same message, a short example to make it clearer.PHP Code
<?php
// In PHP 5.6
assert(1 == 2, "They are not equals");
assert(1 == 3, "They are not equals");
// In PHP 7
assert(1 == 2, new AssertionError());
assert(1 == 3, new AssertionError());
The main advantage is that you don't need to write again and again the same message, you can now return any class that implement the Throwable interface. Making it easier to test your script.
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
Mysql_* is dead
To conclude this short presentation of the new feature, we will talk about the best decision made on this release. The suppression of mysql_* function.You are now forced to switch to PDO or MySQLi, and it make me feel good, really good.
[img]http://nsa37.casimages.com/img/2015/12/30/151230044751619202.png[/img]
And way more...
Of course this list was not exhaustive, they are a lot of new feature / addition to current feature to explore like Yield, Anonymous Class, Reflection and much more... But it's up to you read to this if you're interested. I tried to make a short list of the features that I think, you'll use very soon.Thanks to Haxalot for the reference guide linked in Yield, Anonymous class and Reflection




![[Image: s0lKFns.gif]](https://i.imgur.com/s0lKFns.gif)

![[Image: vibesignature.gif]](https://i.ibb.co/8LvxwtYQ/vibesignature.gif)
![[Image: vjPWhyy.gif]](https://i.imgur.com/vjPWhyy.gif)
![[Image: empire.gif]](https://i.tiny.re/empire.gif)