PHP7
Speaker: Mr.L (Zendvn)
The PHP 7 Story
- PHP 7 is the first major PHP release since PHP 5.0.0, which was released in
2004, more than 11 years ago.
- PHP 6 was canceled in 2010.
- In 2010 Facebook announced the PHP HipHop compiler
- HHVM (HipHop Virtual Machine) which would compile PHP into native
machine code using a JIT engine (Just In Time)
Performance Improvements
- The core refactoring introduced by the phpng RFC makes PHP 7 as fast as
(or faster than) HHVM
- Most real world applications running on PHP 5.6 will run at least twice as fast
on PHP 7
Benchmarks
http://talks.php.net/afup15#/boxspecs
Major New Features of PHP 7
Deprecated Items Removed
- ASP-style tags ( <%, <%= and %> )
- script tags (<script language=”php”> )
- Other functions that were previously deprecated, like split, have also been
removed in PHP 7
- The ereg extension (and all ereg_* functions) should be replaced with the
PCRE extension (preg_* functions)
- The mysql extension (and the mysql_* functions should be used mysqli
extension and the mysqli_*functions instead
Uniform Variable Syntax
<?php
class Person
{
public $name = Zendvn;
public $job = 'Developer;
}
$person = new Person();
$property = [ 'first' => 'name', 'second' => 'info' ];
echo "nMy name is " . $person->$property['first'] . "nn";
Uniform Variable Syntax
- In PHP 5, the expression $person->$property['first'] is evaluated as $person->
{$property['first']}
- In PHP 7, the expression $person->$property['first'] is evaluated as {$person-
>$property}['first']
- A quick and easy way to fix this problem: $person->{$property['first']} many
expressions previously treated as invalid will now become valid
<?php
class Person
{
public static $company = 'Zendvn';
public function getFriends()
{
return [
'Khanh' => function () {
return 'Zend 1 and Wordpress';
},
'Lan' => function () {
return 'PHP and Android';
}
];
}
public function getFriendsOf($someone)
{
return $this->getFriends()[$someone];
}
public static function getNewPerson()
{
return new Person();
}
}
Uniform Variable Syntax
With PHP 5:
$person = new Person();
$friends = $person->getFriends();
$course = $friends['Khanh'];
echo "n" . $course() . "nn";
$course = $person->getFriendsOf('Lan');
echo "n" . $course() . "nn";
Uniform Variable Syntax
With PHP 7, we can create nested associations and different combinations
between operators:
$person = new Person();
echo "n" . $person->getFriends()['Khanh']() . "nn";
echo "n" . $person->getFriendsOf('Lan')() . "nn";
Similarly, nested static access is also possible:
echo "n" . $person::getNewPerson()::$company . "nn";
Fatal Error with multiple “default” clauses
switch ($expr) {
default:
echo "Hello World";
break;
default:
echo "Goodbye Moon!";
break;
}
In PHP 5, the last default would be used
But in PHP 7 you will now get a Fatal Error: Switch statements may only contain
one default clause.
Engine Exceptions
<?php
set_error_handler(function ($code, $message) {
echo "ERROR $code: " . $message . "nn";
});
function a(ArrayObject $b){
return $b;
}
a("test");
echo "Hello World";
Engine Exceptions
In PHP 5:
ERROR 4096: Argument 1 passed to a() must be an instance of ArrayObject, string given…
Hello World
In PHP 7, this code generates a TypeError exception:
Fatal error: Uncaught TypeError: Argument 1 passed to a() must be an instance of ArrayObject, string given,
called in /vagrant/tests/test04.php on line 12 and defined in /vagrant/tests/test04.php:7
Stack trace:
#0 /vagrant/tests/test04.php(12): a('test')
#1 {main}
thrown in /vagrant/tests/test04.php on line 7
Exception hierarchy
Throwable interface:
● Exception implements Throwable
○ ErrorException extends Exception
○ RuntimeException extends Exception
● Error implements Throwable
○ TypeError extends Error
○ ParseError extends Error
○ AssertionError extends Error
New Operators
- Spaceship Operator
- Coalesce Operator
Spaceship Operator
function cmp_php5($a, $b) {
return ($a < $b) ? -1 : (($a >$b) ? 1 : 0);
}
function cmp_php7($a, $b) {
return $a <=> $b;
}
Coalesce Operator
$a = NULL;
$b = 0;
$c = 2;
PHP 5:
echo isset($a) ? $a : b; // 0
PHP 7:
echo $a ?? $b; // 0
echo $c ?? $b; // 2
echo $a ?? $b ?? $c; // 0
echo $a ?? $x ?? $c; // 2
Scalar Type Hints
<?php
function double(int $value)
{
return 2 * $value;
}
$a = double("5");
var_dump($a);
- Finally make it possible to use integers, floats, strings, and booleans as type
hints for functions and methods.
- By default, scalar type hints are non-restrictive.
Scalar Type Hints
we can enable strict mode by including the directive declare(strict_types = 1)
<?php
declare(strict_types = 1);
function double(int $value)
{
return 2 * $value;
}
$a = double("5");
var_dump($a);
This code will generate a Fatal error: Uncaught TypeError: Argument 1 passed to
double() must be of the type integer, string given.
Return Type Hints
<?php
function a() : bool
{
return 1;
}
var_dump(a());
Fatal error: Uncaught TypeError: Return value of a() must be of the
type boolean, integer returned
The addition of a new escape character, u, allows us to specify Unicode character
code points (in hexidecimal) unambiguously inside PHP strings:
The syntax used is u{CODEPOINT}, for example the green heart, , can be
expressed as the PHP string: "u{1F49A}".
Unicode Codepoint Escape Syntax
Anonymous Classes
<?php
class SomeClass {}
interface SomeInterface {}
trait SomeTrait {}
var_dump(new class(10) extends SomeClass implements SomeInterface {
private $num;
public function __construct($num){
$this->num = $num;
}
use SomeTrait;
});
With PHP 5.4 we saw the addition of Closure->bindTo() and Closure::
bind()
$f = function () {
return $this->n;
};
class MyClass {
private $n = 42;
}
$myC = new MyClass;
$c = $f->bindTo($myC, "MyClass");
$c();
Bind Closure on Call
PHP 7 now adds an easy way to do this at call time, binding both $this
and the calling scope to the same object with the addition of Closure-
>call()
$f = function () {
return $this->n;
};
class MyClass {
private $n = 42;
}
$myC = new MyClass;
$f->call($myC);
Bind Closure on Call
// Original
use FrameworkComponentSubComponentClassA;
use FrameworkComponentSubComponentClassB as ClassC;
use FrameworkComponentOtherComponentClassD;
// With Group Use
use FrameworkComponent{
SubComponentClassA,
SubComponentClassB as ClassC,
OtherComponentClassD
};
Group Use Declarations
- Docker:
https://github.com/janatzend/docker-php7-nightly-build
- Vagrant:
https://github.com/rlerdorf/php7dev.git
Test your application
Link
- http://talks.php.net/afup15#/
- https://github.com/rlerdorf/php7dev
- https://www.digitalocean.com/company/blog/getting-ready-for-php-7/
- https://blog.engineyard.com/2015/what-to-expect-php-7-2
THANKS!!!

Giới thiệu PHP 7

  • 1.
  • 2.
    The PHP 7Story - PHP 7 is the first major PHP release since PHP 5.0.0, which was released in 2004, more than 11 years ago. - PHP 6 was canceled in 2010. - In 2010 Facebook announced the PHP HipHop compiler - HHVM (HipHop Virtual Machine) which would compile PHP into native machine code using a JIT engine (Just In Time)
  • 3.
    Performance Improvements - Thecore refactoring introduced by the phpng RFC makes PHP 7 as fast as (or faster than) HHVM - Most real world applications running on PHP 5.6 will run at least twice as fast on PHP 7
  • 4.
  • 5.
  • 6.
    Deprecated Items Removed -ASP-style tags ( <%, <%= and %> ) - script tags (<script language=”php”> ) - Other functions that were previously deprecated, like split, have also been removed in PHP 7 - The ereg extension (and all ereg_* functions) should be replaced with the PCRE extension (preg_* functions) - The mysql extension (and the mysql_* functions should be used mysqli extension and the mysqli_*functions instead
  • 7.
    Uniform Variable Syntax <?php classPerson { public $name = Zendvn; public $job = 'Developer; } $person = new Person(); $property = [ 'first' => 'name', 'second' => 'info' ]; echo "nMy name is " . $person->$property['first'] . "nn";
  • 8.
    Uniform Variable Syntax -In PHP 5, the expression $person->$property['first'] is evaluated as $person-> {$property['first']} - In PHP 7, the expression $person->$property['first'] is evaluated as {$person- >$property}['first'] - A quick and easy way to fix this problem: $person->{$property['first']} many expressions previously treated as invalid will now become valid
  • 9.
    <?php class Person { public static$company = 'Zendvn'; public function getFriends() { return [ 'Khanh' => function () { return 'Zend 1 and Wordpress'; }, 'Lan' => function () { return 'PHP and Android'; } ]; } public function getFriendsOf($someone) { return $this->getFriends()[$someone]; } public static function getNewPerson() { return new Person(); } }
  • 10.
    Uniform Variable Syntax WithPHP 5: $person = new Person(); $friends = $person->getFriends(); $course = $friends['Khanh']; echo "n" . $course() . "nn"; $course = $person->getFriendsOf('Lan'); echo "n" . $course() . "nn";
  • 11.
    Uniform Variable Syntax WithPHP 7, we can create nested associations and different combinations between operators: $person = new Person(); echo "n" . $person->getFriends()['Khanh']() . "nn"; echo "n" . $person->getFriendsOf('Lan')() . "nn"; Similarly, nested static access is also possible: echo "n" . $person::getNewPerson()::$company . "nn";
  • 12.
    Fatal Error withmultiple “default” clauses switch ($expr) { default: echo "Hello World"; break; default: echo "Goodbye Moon!"; break; } In PHP 5, the last default would be used But in PHP 7 you will now get a Fatal Error: Switch statements may only contain one default clause.
  • 13.
    Engine Exceptions <?php set_error_handler(function ($code,$message) { echo "ERROR $code: " . $message . "nn"; }); function a(ArrayObject $b){ return $b; } a("test"); echo "Hello World";
  • 14.
    Engine Exceptions In PHP5: ERROR 4096: Argument 1 passed to a() must be an instance of ArrayObject, string given… Hello World In PHP 7, this code generates a TypeError exception: Fatal error: Uncaught TypeError: Argument 1 passed to a() must be an instance of ArrayObject, string given, called in /vagrant/tests/test04.php on line 12 and defined in /vagrant/tests/test04.php:7 Stack trace: #0 /vagrant/tests/test04.php(12): a('test') #1 {main} thrown in /vagrant/tests/test04.php on line 7
  • 15.
    Exception hierarchy Throwable interface: ●Exception implements Throwable ○ ErrorException extends Exception ○ RuntimeException extends Exception ● Error implements Throwable ○ TypeError extends Error ○ ParseError extends Error ○ AssertionError extends Error
  • 16.
    New Operators - SpaceshipOperator - Coalesce Operator
  • 17.
    Spaceship Operator function cmp_php5($a,$b) { return ($a < $b) ? -1 : (($a >$b) ? 1 : 0); } function cmp_php7($a, $b) { return $a <=> $b; }
  • 18.
    Coalesce Operator $a =NULL; $b = 0; $c = 2;
  • 19.
    PHP 5: echo isset($a)? $a : b; // 0 PHP 7: echo $a ?? $b; // 0 echo $c ?? $b; // 2 echo $a ?? $b ?? $c; // 0 echo $a ?? $x ?? $c; // 2
  • 20.
    Scalar Type Hints <?php functiondouble(int $value) { return 2 * $value; } $a = double("5"); var_dump($a); - Finally make it possible to use integers, floats, strings, and booleans as type hints for functions and methods. - By default, scalar type hints are non-restrictive.
  • 21.
    Scalar Type Hints wecan enable strict mode by including the directive declare(strict_types = 1) <?php declare(strict_types = 1); function double(int $value) { return 2 * $value; } $a = double("5"); var_dump($a); This code will generate a Fatal error: Uncaught TypeError: Argument 1 passed to double() must be of the type integer, string given.
  • 22.
    Return Type Hints <?php functiona() : bool { return 1; } var_dump(a()); Fatal error: Uncaught TypeError: Return value of a() must be of the type boolean, integer returned
  • 23.
    The addition ofa new escape character, u, allows us to specify Unicode character code points (in hexidecimal) unambiguously inside PHP strings: The syntax used is u{CODEPOINT}, for example the green heart, , can be expressed as the PHP string: "u{1F49A}". Unicode Codepoint Escape Syntax
  • 24.
    Anonymous Classes <?php class SomeClass{} interface SomeInterface {} trait SomeTrait {} var_dump(new class(10) extends SomeClass implements SomeInterface { private $num; public function __construct($num){ $this->num = $num; } use SomeTrait; });
  • 25.
    With PHP 5.4we saw the addition of Closure->bindTo() and Closure:: bind() $f = function () { return $this->n; }; class MyClass { private $n = 42; } $myC = new MyClass; $c = $f->bindTo($myC, "MyClass"); $c(); Bind Closure on Call
  • 26.
    PHP 7 nowadds an easy way to do this at call time, binding both $this and the calling scope to the same object with the addition of Closure- >call() $f = function () { return $this->n; }; class MyClass { private $n = 42; } $myC = new MyClass; $f->call($myC); Bind Closure on Call
  • 27.
    // Original use FrameworkComponentSubComponentClassA; useFrameworkComponentSubComponentClassB as ClassC; use FrameworkComponentOtherComponentClassD; // With Group Use use FrameworkComponent{ SubComponentClassA, SubComponentClassB as ClassC, OtherComponentClassD }; Group Use Declarations
  • 28.
  • 29.
    Link - http://talks.php.net/afup15#/ - https://github.com/rlerdorf/php7dev -https://www.digitalocean.com/company/blog/getting-ready-for-php-7/ - https://blog.engineyard.com/2015/what-to-expect-php-7-2
  • 30.