giới thiệu php 7

30
PHP7 Speaker: Mr.L (Zendvn)

Upload: zendvn

Post on 12-Apr-2017

573 views

Category:

Education


0 download

TRANSCRIPT

Page 1: Giới thiệu PHP 7

PHP7Speaker: Mr.L (Zendvn)

Page 2: Giới thiệu PHP 7

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)

Page 3: Giới thiệu PHP 7

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

Page 4: Giới thiệu PHP 7

Benchmarks

http://talks.php.net/afup15#/boxspecs

Page 5: Giới thiệu PHP 7

Major New Features of PHP 7

Page 6: Giới thiệu 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

Page 7: Giới thiệu PHP 7

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";

Page 8: Giới thiệu PHP 7

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

Page 9: Giới thiệu PHP 7

<?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(); }}

Page 10: Giới thiệu PHP 7

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";

Page 11: Giới thiệu PHP 7

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";

Page 12: Giới thiệu PHP 7

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.

Page 13: Giới thiệu PHP 7

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";

Page 14: Giới thiệu PHP 7

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

Page 15: Giới thiệu PHP 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

Page 16: Giới thiệu PHP 7

New Operators

- Spaceship Operator

- Coalesce Operator

Page 17: Giới thiệu PHP 7

Spaceship Operator

function cmp_php5($a, $b) { return ($a < $b) ? -1 : (($a >$b) ? 1 : 0);}

function cmp_php7($a, $b) { return $a <=> $b;}

Page 18: Giới thiệu PHP 7

Coalesce Operator

$a = NULL;$b = 0;$c = 2;

Page 19: Giới thiệu PHP 7

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

Page 20: Giới thiệu PHP 7

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.

Page 21: Giới thiệu PHP 7

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.

Page 22: Giới thiệu PHP 7

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

Page 23: Giới thiệu PHP 7

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

Page 24: Giới thiệu PHP 7

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;

});

Page 25: Giới thiệu PHP 7

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

Page 26: Giới thiệu PHP 7

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

Page 27: Giới thiệu PHP 7

// 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

Page 28: Giới thiệu PHP 7

- Docker:

https://github.com/janatzend/docker-php7-nightly-build

- Vagrant:

https://github.com/rlerdorf/php7dev.git

Test your application

Page 30: Giới thiệu PHP 7

THANKS!!!