Upload
swiftotter-studios
View
459
Download
1
Embed Size (px)
Citation preview
WELCOME!
MY FAMILY
HONEY BEES
NEW TECHNOLOGY
WITH MUCH GRATITUDE.• Many, many hours represented:
• 189 people
• 10,033 commits
• All to give us a better programming language.
5+1=
5+1=7
PHP6?
HTTPS://PHILSTURGEON.UK/ PHP/ 2014/
07/ 23/
NEVERENDING-MUPPET- DEBATE-OF-PHP-6-V-PHP-7
HTTPS://3V4L.ORG/“EVAL”
NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION
NULL COALESCEhttps://wiki.php.net/rfc/isset_ternary
$action = isset($_GET[‘action’])
? $_GET[‘action’]
: ‘default’;
< PHP 7.0
$action = isset($_GET[‘action’]) ? $_GET[‘action’] : (isset($_POST[‘action’]) ? $_POST[‘action’] : ($this->getAction() ? $this->getAction() : $_’default’ ) );
??
$_GET[‘value’] ?? $_POST[‘value’];
$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;
$key = (string) $key; $keyName = $this->getAlias($key) ?? $key;
return $this->params[$keyName]
?? $this->queryParams[$keyName]
?? $this->postParams[$keyName];
PHP 7
10 > 3 LINES. NOT BAD.
IMPROVED STRONG TYPING SUPPORThttps://wiki.php.net/rfc/uniform_variable_syntax
https://wiki.php.net/rfc/return_types
function calculateShippingTo($postalCode) { $postalCode = (int)$postalCode; }
calculateShippingTo(“66048”);
< PHP 7
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
function calculateShippingTo(int $postalCode) { // … }
calculateShippingTo(“66048”); // $postalCode is cast to an integer
PHP 7
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…
function test(string $val) { echo $val; }
test(new class { public function __toString() { return "Hey, PHP[tek]!"; } });
// Outputs: string(14) "Hey, PHP[tek]!"
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
FUNCTION RETURN TYPES• Return type goes after the function declaration:
function combine($str1, $str2): string { // … }
function addProduct(): \Product { // … }
function calculateShippingTo(int $postalCode): float { return 5.11; }
var_dump(echo calculateShippingTo(“66048”)); // float(5.11)
PHP 7
FAST-FORWARD:https://wiki.php.net/rfc/union_types
https://wiki.php.net/rfc/typed-properties
SPACESHIP OPERATORhttps://wiki.php.net/rfc/combined-comparison-operator
<?php
usort($values, function($a, $b) { if ($a == $b) { return 0; } return ($a < $b) ? -1 : 1; });
< PHP 7
<=>
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
<?php
usort($values, function($a, $b) { return $a <=> $b; });
PHP 7
HTTPS://3V4L.ORG/
96Cd4
GROUPED USE DECLARATIONS
https://wiki.php.net/rfc/group_use_declarations
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
use Magento\Framework\Stdlib\{ Cookie\CookieReaderInterface, StringUtils };use Zend\Http\Header\HeaderInterface;use Zend\Stdlib\{Parameters, ParametersInterface};use Zend\Uri\{UriFactory, UriInterface};
PHP 7
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
ANONYMOUS CLASSES
https://wiki.php.net/rfc/anonymous_classes
return new class extends \Product { function getWeight() { return 5; } });
// code testing, slight modifications // to classes
ANONYMOUS CLASSES
CLOSURE::CALL
https://wiki.php.net/rfc/closure_apply
$closure = function($context) { return $context->value . "\n"; };
$a = new class { public $value = "Hello"; };
$closure($a); // Output: "Hello"
PHP 5.3
$closure = function() { return $this->value . "\n"; };
$a = new class { public $value = "Hello"; }; $aBound = $closure->bindTo($a);
$closure(); // Output: "Hello"
< PHP 7
$canVoidOrder = function() { // executed within the $order context return $this->_canVoidOrder(); }
$canVoidOrder = $canVoidOrder->bindTo($order, $order); echo $canVoidOrder();
MAGENTO
$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
GENERATOR DELEGATION
https://wiki.php.net/rfc/generator-delegation
function generator1() { for ($i = 0; $i < 5; $i++) { yield $i; }
for ($i = 0; $i < generator2(); $i++) { yield $i;
} }
function generator2() { /** … **/ }
< PHP 7
function mergeOutput($gen1, $gen2) { $array1 = iterator_to_array($gen1); $array2 = iterator_to_array($gen2);
return array_merge($array1, $array2); }
< PHP 7
function generator1() { for ($i=0; $i<5; $i++) { yield $i; } yield from generator2();
}
function generator2() { /** … **/ }
// array_merge for generators.
PHP 7
NULL COALESCE
STRONG-TYPE SUPPORT
SPACESHIP OPERATOR
GROUPED USE DECLARATIONS
ANONYMOUS CLASSES
CLOSURE::CALL
GENERATOR DELEGATION
NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION
PHP7 PERFORMANCESource: http://www.lornajane.net/posts/2015/php-7-benchmarks
EXECUTION ORDER CHANGEShttps://wiki.php.net/rfc/uniform_variable_syntax
$$foo[‘bar’]->baz();
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.
$foo->$bar[‘baz’];{#1
$foo->result;{#2
PHP 5.6 {
#1
$result[‘baz’];{#2
PHP 7$foo->$bar[‘baz’];
HTTPS://3V4L.ORG/
WN37p 1ObtsPHP 5.6 PHP 7
getClass()::CONST_NAME;PHP 7
getClosure()();PHP 7
EXCEPTIONS• Exceptions now inherit the new Throwable interface.
try {} catch (Exception $e){ /***/ }
try {} catch (Throwable $e) { /***/ }
EXCEPTIONS• New Error class for:
• ArithmeticError
• DivisionByZeroError
• AssertionError
• ParseError
• TypeError
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
FILTERED UNSERIALIZATIONS
https://wiki.php.net/rfc/secure_unserialize
UNSERIALIZE• Now takes an additional argument: $options
• allowed_classes:
• false: don’t unserialize any classes
• true: unserialize all classes
• array: unserialize specified classes
unserialize($value, [ ‘allowed_classes’ => false ]);
unserialize($value, [ ‘allowed_classes’ => [ ‘Blue\\Models\\UserModel’ ] ]);
PHP 7
NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION
BREAKING CHANGES
BREAKING CHANGES• mysql_/ereg functions removed.
• https://pecl.php.net/package/mysql
• Calling a non-static function statically.
• ASP-style tags
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
HTTP://PHP.NET/ MANUAL/ EN/MIGRATION70.INCOMPATIBLE.PHP
NEW FEATURES UPDATES BREAKING CHANGES IMPLEMENTATION
PHP7 HAS NOT BEEN WIDELY
ADOPTED. YET.
0.7% ADOPTION SO FAR.
Source: https://w3techs.com/technologies/details/pl-php/all/all
WE CAN CHANGE THAT.
UPGRADE CONSIDERATIONS• PHP7 is production-ready.
• Few have adopted it.
• If you release, you may encounter issues that haven’t been discussed online.
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.
• 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
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
TAKE-AWAYS• Take advantage of the new features.
• Upgrade.
• Encourage your friends about PHP7.
• Together, we will get the industry moved to PHP7!
THANK YOU!