what's new with php7

84

Upload: swiftotter-studios

Post on 13-Apr-2017

457 views

Category:

Software


1 download

TRANSCRIPT

Page 1: What's new with PHP7
Page 2: What's new with PHP7

WELCOME!

Page 3: What's new with PHP7

MY FAMILY

Page 4: What's new with PHP7

HONEY BEES

Page 5: What's new with PHP7

NEW TECHNOLOGY

Page 6: What's new with PHP7

WITH MUCH GRATITUDE.• Many, many hours represented:

• 189 people

• 10,033 commits

• All to give us a better programming language.

Page 7: What's new with PHP7

5+1=

Page 8: What's new with PHP7

5+1=7

Page 9: What's new with PHP7

PHP6?

Page 10: What's new with PHP7

HTTPS://PHILSTURGEON.UK/ PHP/ 2014/

07/ 23/

NEVERENDING-MUPPET- DEBATE-OF-PHP-6-V-PHP-7

Page 11: What's new with PHP7

HTTPS://3V4L.ORG/“EVAL”

Page 12: What's new with PHP7

NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION

Page 13: What's new with PHP7

NULL COALESCEhttps://wiki.php.net/rfc/isset_ternary

Page 14: What's new with PHP7

$action = isset($_GET[‘action’])

? $_GET[‘action’]

: ‘default’;

< PHP 7.0

Page 15: What's new with PHP7

$action = isset($_GET[‘action’]) ? $_GET[‘action’] : (isset($_POST[‘action’]) ? $_POST[‘action’] : ($this->getAction() ? $this->getAction() : $_’default’ ) );

Page 16: What's new with PHP7

??

Page 17: What's new with PHP7

$_GET[‘value’] ?? $_POST[‘value’];

Page 18: What's new with PHP7

$key = (string) $key;$keyName =

(null !== ($alias = $this->getAlias($key))) ? $alias : $key;

if (isset($this->params[$keyName])) { return $this->params[$keyName];} elseif (isset($this->queryParams[$keyName])) { return $this->queryParams[$keyName]; } elseif (isset($this->postParams[$keyName])) { return $this->postParams[$keyName]; }return $default;

Page 19: What's new with PHP7

$key = (string) $key; $keyName = $this->getAlias($key) ?? $key;

return $this->params[$keyName]

?? $this->queryParams[$keyName]

?? $this->postParams[$keyName];

PHP 7

Page 20: What's new with PHP7

10 > 3 LINES. NOT BAD.

Page 21: What's new with PHP7

IMPROVED STRONG TYPING SUPPORThttps://wiki.php.net/rfc/uniform_variable_syntax

https://wiki.php.net/rfc/return_types

Page 22: What's new with PHP7

function calculateShippingTo($postalCode) { $postalCode = (int)$postalCode; }

calculateShippingTo(“66048”);

< PHP 7

Page 23: What's new with PHP7

Class/interface name (PHP 5.0) self (PHP 5.0) array (PHP 5.1) callable (PHP 5.4)

bool (PHP 7) float (PHP 7) int (PHP 7) string (PHP 7)

Source: http://php.net/manual/en/functions.arguments.php#functions.arguments.type-declaration

Page 24: What's new with PHP7

function calculateShippingTo(int $postalCode) { // … }

calculateShippingTo(“66048”); // $postalCode is cast to an integer

PHP 7

Page 25: What's new with PHP7

function test(string $val) { echo $val; }

test(new class {});

// Can’t convert: Fatal error: Uncaught TypeError: Argument 1 passed to test() must be of the type string, object given…

Page 26: What's new with PHP7

function test(string $val) { echo $val; }

test(new class { public function __toString() { return "Hey, PHP[tek]!"; } });

// Outputs: string(14) "Hey, PHP[tek]!"

Page 27: What's new with PHP7

function calculateShippingTo(int $postalCode) { // … }

//** separate file: **//

declare(strict_types = 1); calculateShippingTo(“66048”);

// Throws: Catchable fatal error: Argument 1 passed to calculateShippingTo() must be of the type integer, string given

Page 28: What's new with PHP7

FUNCTION RETURN TYPES• Return type goes after the function declaration:

function combine($str1, $str2): string { // … }

function addProduct(): \Product { // … }

Page 29: What's new with PHP7

function calculateShippingTo(int $postalCode): float { return 5.11; }

var_dump(echo calculateShippingTo(“66048”)); // float(5.11)

PHP 7

Page 30: What's new with PHP7

FAST-FORWARD:https://wiki.php.net/rfc/union_types

https://wiki.php.net/rfc/typed-properties

Page 31: What's new with PHP7

SPACESHIP OPERATORhttps://wiki.php.net/rfc/combined-comparison-operator

Page 32: What's new with PHP7

<?php

usort($values, function($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; });

< PHP 7

Page 33: What's new with PHP7

<=>

Page 34: What's new with PHP7

SPACESHIP OPERATOR• Works with anything,

including arrays.

• 0 if equal

• 1 if left > right

• -1 if left < right

echo 6 <=> 6;

// Result: 0

echo 7 <=> 6;

// Result: 1

echo 6 <=> 7;

// Result: -1

Page 35: What's new with PHP7

<?php

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

PHP 7

Page 36: What's new with PHP7

HTTPS://3V4L.ORG/

96Cd4

Page 37: What's new with PHP7

GROUPED USE DECLARATIONS

https://wiki.php.net/rfc/group_use_declarations

Page 38: What's new with PHP7

use Magento\Framework\Stdlib\Cookie\CookieReaderInterface; use Magento\Framework\Stdlib\StringUtils; use Zend\Http\Header\HeaderInterface; use Zend\Stdlib\Parameters; use Zend\Stdlib\ParametersInterface; use Zend\Uri\UriFactory; use Zend\Uri\UriInterface;

< PHP 7

Page 39: What's new with PHP7

use Magento\Framework\Stdlib\{ Cookie\CookieReaderInterface, StringUtils };use Zend\Http\Header\HeaderInterface;use Zend\Stdlib\{Parameters, ParametersInterface};use Zend\Uri\{UriFactory, UriInterface};

PHP 7

Page 40: What's new with PHP7

use Magento\Framework\Stdlib\{ Cookie\CookieReaderInterface, StringUtils as Utils };use Zend\Http\Header\HeaderInterface;use Zend\Stdlib\{Parameters, ParametersInterface};use Zend\Uri\{UriFactory, UriInterface};

PHP 7

Page 41: What's new with PHP7

ANONYMOUS CLASSES

https://wiki.php.net/rfc/anonymous_classes

Page 42: What's new with PHP7

return new class extends \Product { function getWeight() { return 5; } });

// code testing, slight modifications // to classes

ANONYMOUS CLASSES

Page 43: What's new with PHP7

CLOSURE::CALL

https://wiki.php.net/rfc/closure_apply

Page 44: What's new with PHP7

$closure = function($context) { return $context->value . "\n"; };

$a = new class { public $value = "Hello"; };

$closure($a); // Output: "Hello"

PHP 5.3

Page 45: What's new with PHP7

$closure = function() { return $this->value . "\n"; };

$a = new class { public $value = "Hello"; }; $aBound = $closure->bindTo($a);

$closure(); // Output: "Hello"

< PHP 7

Page 46: What's new with PHP7

$canVoidOrder = function() { // executed within the $order context return $this->_canVoidOrder(); }

$canVoidOrder = $canVoidOrder->bindTo($order, $order); echo $canVoidOrder();

MAGENTO

Page 47: What's new with PHP7

$closure = function() { return $this->value . "\n"; };

$a = new class { public $value = "Hello"; }; $b = new class { public $value = "Test"; };

echo $closure->call($a); // Output: "Hello" echo $closure->call($b); // Output: "Test"

PHP 7

Page 48: What's new with PHP7

GENERATOR DELEGATION

https://wiki.php.net/rfc/generator-delegation

Page 49: What's new with PHP7

function generator1() { for ($i = 0; $i < 5; $i++) { yield $i; }

for ($i = 0; $i < generator2(); $i++) { yield $i;

} }

function generator2() { /** … **/ }

< PHP 7

Page 50: What's new with PHP7

function mergeOutput($gen1, $gen2) { $array1 = iterator_to_array($gen1); $array2 = iterator_to_array($gen2);

return array_merge($array1, $array2); }

< PHP 7

Page 51: What's new with PHP7

function generator1() { for ($i=0; $i<5; $i++) { yield $i; } yield from generator2();

}

function generator2() { /** … **/ }

// array_merge for generators.

PHP 7

Page 52: What's new with PHP7

NULL COALESCE

STRONG-TYPE SUPPORT

SPACESHIP OPERATOR

GROUPED USE DECLARATIONS

ANONYMOUS CLASSES

CLOSURE::CALL

GENERATOR DELEGATION

Page 53: What's new with PHP7

NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION

Page 54: What's new with PHP7

PHP7 PERFORMANCESource: http://www.lornajane.net/posts/2015/php-7-benchmarks

Page 55: What's new with PHP7
Page 56: What's new with PHP7

EXECUTION ORDER CHANGEShttps://wiki.php.net/rfc/uniform_variable_syntax

Page 57: What's new with PHP7

$$foo[‘bar’]->baz();

Page 58: What's new with PHP7

EXECUTION ORDER CHANGES• PHP <= 5.6 had a long list of execution order rules:

• Most of the time, the parser executed from left to right.

• Breaking changes for some frameworks and some code.

Page 59: What's new with PHP7

$foo->$bar[‘baz’];{#1

$foo->result;{#2

PHP 5.6 {

#1

$result[‘baz’];{#2

PHP 7$foo->$bar[‘baz’];

Page 60: What's new with PHP7

HTTPS://3V4L.ORG/

WN37p 1ObtsPHP 5.6 PHP 7

Page 61: What's new with PHP7

getClass()::CONST_NAME;PHP 7

Page 62: What's new with PHP7

getClosure()();PHP 7

Page 63: What's new with PHP7

EXCEPTIONS• Exceptions now inherit the new Throwable interface.

Page 64: What's new with PHP7

try {} catch (Exception $e){ /***/ }

try {} catch (Throwable $e) { /***/ }

Page 65: What's new with PHP7

EXCEPTIONS• New Error class for:

• ArithmeticError

• DivisionByZeroError

• AssertionError

• ParseError

• TypeError

Page 66: What's new with PHP7

EXCEPTIONStry {

$test = 5 % 0;

} catch (Error $e) {

var_dump($e);

}

// throws DivisionByZeroError

try {

$test = “Test”;

$test->go();

} catch (Error $e) {

var_dump($e);

}

// throws Error

Page 67: What's new with PHP7

FILTERED UNSERIALIZATIONS

https://wiki.php.net/rfc/secure_unserialize

Page 68: What's new with PHP7

UNSERIALIZE• Now takes an additional argument: $options

• allowed_classes:

• false: don’t unserialize any classes

• true: unserialize all classes

• array: unserialize specified classes

Page 69: What's new with PHP7

unserialize($value, [ ‘allowed_classes’ => false ]);

unserialize($value, [ ‘allowed_classes’ => [ ‘Blue\\Models\\UserModel’ ] ]);

PHP 7

Page 70: What's new with PHP7

NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION

Page 71: What's new with PHP7

BREAKING CHANGES

Page 72: What's new with PHP7

BREAKING CHANGES• mysql_/ereg functions removed.

• https://pecl.php.net/package/mysql

• Calling a non-static function statically.

• ASP-style tags

Page 73: What's new with PHP7

BREAKING CHANGES• list() assigns variables in proper order now:

• call_user_method() and call_user_method_array() removed.

list($a[], $a[], $a[]) = [1, 2, 3]; // PHP 5.6 output: 3, 2, 1 // PHP 7 output: 1, 2, 3

Page 74: What's new with PHP7

HTTP://PHP.NET/ MANUAL/ EN/MIGRATION70.INCOMPATIBLE.PHP

Page 75: What's new with PHP7

NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION

Page 76: What's new with PHP7

PHP7 HAS NOT BEEN WIDELY

ADOPTED. YET.

Page 77: What's new with PHP7

0.7% ADOPTION SO FAR.

Source: https://w3techs.com/technologies/details/pl-php/all/all

Page 78: What's new with PHP7

WE CAN CHANGE THAT.

Page 79: What's new with PHP7

UPGRADE CONSIDERATIONS• PHP7 is production-ready.

• Few have adopted it.

• If you release, you may encounter issues that haven’t been discussed online.

Page 80: What's new with PHP7

UPGRADE CONSIDERATIONS• What removed functions does your code depend on?

• https://github.com/sstalle/php7cc

• Run your unit tests against your code on a box with PHP7.

• Try to release on a new server:

• Will give you a backup if something major happens.

Page 81: What's new with PHP7

• puphpet.com: choose PHP 7 as the version to install.

• https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-centos-7

• https://www.digitalocean.com/community/tutorials/how-to-upgrade-to-php-7-on-ubuntu-14-04

INSTALLING PHP7

Page 82: What's new with PHP7

FURTHER READING• https://leanpub.com/php7/read

• Excellent read about every new PHP 7 feature.

• http://php.net/manual/en/migration70.new-features.php

• PHP’s documentation

Page 83: What's new with PHP7

TAKE-AWAYS• Take advantage of the new features.

• Upgrade.

• Encourage your friends about PHP7.

• Together, we will get the industry moved to PHP7!

Page 84: What's new with PHP7

THANK YOU!