Daniel Gafitescu Freelance web developer focused on open source solutions

19Dec/150

​ O’Reilly – Up to Speed with PHP 7

Recently I had updated my VPS Php version to 7. It was easy and pretty smooth. I only had to add a single file so that WordPress would work with mysqli extension instead of old mysql extension which was deprecated.
I wanted to see what's new in Php 7 and I saw that O'Reilly had a course about it so I went and what it. Here is a short review of the course.


Php 7
- first major version release since PHP5 in 2004 after Php6 was abandoned in 2010
- is powered by a new engine PHPNG ( Php new generation)

Changes :

Internal changes
- according to Rasmus Lerdorf 100%+ performance on the most real-world apps
- lower memory usage
on Wodpress 4.1.1 PHP7 is slightly slower than HHVM 3.6.1
- simplifies creating add-on tools
- yield doesn’t require parentheses
- empty list() now allowed
- 64-bit integer support in Windows
- support for strings >= 2^31 on 64-bit builds ( video file conversion as string )
- very few breaks in backward compatibility
- most fatal errors now catchable ( Exception being thrown)
- mysql extension has been deprecated
- new json extension
- assert controller by configuration settings
- posix regex has been removed
- alternative php tags have been removed
- assign of new by reference has been removed

New operators:
- Spaceship operator ( already in Perl & Ruby )

$a = 6;
$b = $c = 10;
echo ($a <=> $b); ==> -1
echo ($b <=> $c ); ==> 0
echo ($c <=> $a) ==> 1

- Null coalesce operator

$variable = $non_existent ?? ‘default’ ;
also can be used in cascade
$var = $non_existent ?? $name ?? ‘default’;

Unicode escape syntax

$name = “Caf\u{00e9}” => Café

Functions
- random_bytes(20) ( Usage bin2hex(random_bytes(20));
- random_int(1,6) ==> generate a number between 1 and 6

Return type declaration
- is optional
prevent unintended return values
- forces sub classes to return expected data type

function convertor($string) : array {
……
return $array;
}

- Scalar type hinting
used for boolean (bool ), integer(int), float & string
- optional
- have 2 modes week & strong ( is done by declare(strict_types=1);
and needs to be first statement in the file

function multiply(int $a, int $b): int {
return $a*$b;
}

Anonymous classes
-can be assign to a variable
-return from a function
-pass as argument to a function
-extend a class , implement interface & use traits
- can’t be serialised

$testClass = new class() extends SplHeap {
protect function test($data1,$date2)
{
return ($data1 <=> $data2);
}
}

Generators return expressions


function generate() {
$total = 0;
for($i=1; $i<10;$i++) {
yield $i;
$total += $i;
}
return $total;
}
$counter = generate();
foreach($counter as $number)
{
echo $number;
}
echo “total = “. $counter->getReturn();

Generator delegation;
Binding to closures at call time;
Grouping Use Declarations

use SomeNamesSpace\SomePackage\SomeClass;
use SomeNamesSpace\SomePackage\SomeOtherClass;
use SomeNamesSpace\SomePackage\SomeDifferentClass;

Some as

use SomeNamesSpace\SomePackage\ {SomeClass, SomeOtherClass , SomeDifferentClass};

- Can be used for constants a function as well & even mix and match;

Errors
-catching fatal errors using Error class
-errors and expectations belong to different classes
-both implement the Throwable interface
-errors/exception can be catch in block

Other improvements
-improve security of unserialize;
-preserve 0 fraction when encoding as JSON

Tagged as: , , No Comments