giới thiệu php 7
TRANSCRIPT
PHP7Speaker: 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'] . "\n\n";
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
<?phpclass 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() . "\n\n";
$course = $person->getFriendsOf('Lan');
echo "\n" . $course() . "\n\n";
Uniform Variable Syntax
With PHP 7, we can create nested associations and different combinations between operators:
$person = new Person();echo "\n" . $person->getFriends()['Khanh']() . "\n\n";echo "\n" . $person->getFriendsOf('Lan')() . "\n\n";
Similarly, nested static access is also possible:
echo "\n" . $person::getNewPerson()::$company . "\n\n";
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
<?phpset_error_handler(function ($code, $message) { echo "ERROR $code: " . $message . "\n\n";});
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:7Stack 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; // 0echo $c ?? $b; // 2echo $a ?? $b ?? $c; // 0echo $a ?? $x ?? $c; // 2
Scalar Type Hints
<?phpfunction 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)
<?phpdeclare(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
<?phpfunction 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
// Originaluse Framework\Component\SubComponent\ClassA;use Framework\Component\SubComponent\ClassB as ClassC;use Framework\Component\OtherComponent\ClassD;
// With Group Useuse Framework\Component\{ SubComponent\ClassA, SubComponent\ClassB as ClassC, OtherComponent\ClassD};
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!!!