php7. game changer
Post on 15-Apr-2017
169 Views
Preview:
TRANSCRIPT
© Haim Michael 2011. All Rights Reserved.
PHP7. Game Changer.
Haim MichaelSeptember 22nd, 2016
All logos, trade marks and brand names used in this presentation belong to the respective owners.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Haim Michael Introduction
● Snowboarding. Learning. Coding. Teaching. More than
16 years of Practical Experience.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Haim Michael Introduction
● Professional Certifications
Zend Certified Engineer in PHP
Certified Java Professional
Certified Java EE Web Component Developer
OMG Certified UML Professional
● MBA (cum laude) from Tel-Aviv University
Information Systems Management
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Introduction
PHP was originally developed by Rasmus Lardorf in
1994, and was publicly released in June 1995. This
released version is known as PHP 2.
In 1997 Zeev Suraski & Andi Gutmans rewrote PHP
parser and formed the base of PHP 3.
In 1998 Zeev Suraski & Andi Gutmans started a new
rewrite of PHP core and produced the Zend Engine in
1999.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Introduction
In May 2000 PHP 4 powered by Zend Engine 1.0 was
released.
In July 2004 PHP 5 powered by Zend Engine 2.0 was
released.
PHP 7 was released in November 2015. PHP 7 presents
a significant performance improvement and introduces
more than a few new features and changes. Some of the
changes cause to backwards compatibility breakages.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Performance
PHP 7 is well known for its performance improvement.
The performance of PHP 7 supersedes even the one
introduced by HHVM, Facebook's recently released new
programming language that was developed based on
PHP.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 7
Zend PHP Engine Improvement
The changes that were introduced in Zend's PHP engine
include the use of more compact data structures and
less memory allocations on the heap.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 8
Improvement of 100%
The performance improvement gained in real world
applications varies and depends on the unique
characteristics of each and every web application. In
most cases, the improvement will be around 100%.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 9
Less Memory
In most cases, thanks to the use of more compact data
structures and less memory allocations on the heap, the
memory consumption will be lower.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 10
Future Improvements
The work on improving Zend's PHP engine hasn't
reached to its end.
The additional improvements that can be introduced
into Zend's PHP engine are well listed and wait for their
implementation.
We can expect more performance improvements in the
future.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 11
BenchMark Tests
We can find more than a few benchmarking tests that
show the significant performance improvement in Zend
PHP engine.
https://www.zend.com/en/resources/php7_infographic
http://talks.php.net/oz15#/opencartbench
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Backward Compatibility
Some of the changes introduced by PHP 7 cause
backward compatibility problems.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The <=> Operator
The <=> operator is known as the combined comparison
operator. Its other name is the spaceship operator.
It is a shorthand for performing three way comparisons
on two operands. The returned value is an integer, that
can be either positive, negative or 0.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The <=> Operator
<?php$a = "mama";$b = "abba";echo "<h1>a=$a</h1>";echo "<h1>b=$b</h1>";$temp = $a <=> $b;echo "<h1>temp=$temp</h1>";?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The <=> OperatorLi fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The <=> Operator
When using the <=> operator for comparing strings the
comparison will be a lexicographic one.
We can use this operator for comparing arrays. The
comparison will be between the elements.
We cannot use it for comparing objects.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The ?? Operator
The ?? operator, which is also known as the isset
ternary operator, is a shorthand notation for performing
isset() checks in the ternary operator.
This new operator assists us with those cases in which
we need to check whether the array we work with has a
specific key so we could use its value and if not, then
another value will be used instead.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The ?? Operator
$vec = ['a'=>'abba','b'=>'baba','m'=>'mama'];
//before PHP7//$temp = isset($vec['d'])?$vec['d']:'default';
$temp = $vec['d']??'default';
echo "<h1>$temp</h1>";
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The ?? OperatorLi fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Scalar Type Declaration
As of PHP 7, when declaring a function we can now
specify type for each one of the parameters. We can
specify any of the following scalar types: string, int,
float or bool.
These types come in addition to the types we could
already use as of PHP 5.x including a class name, an
interface name, array or callable.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Scalar Type Declaration
When specifying the types of a function parameters we
can either do it in a coercive mode (default) or a strict
mode.
In order to be in a strict mode we should add the
declare() directive to the beginning of the file. When
in strict mode, if the type check fails then a TypeError
exception will be thrown.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Scalar Type Declaration
The one exception for this behavior is when assigning a
value of the type int to parameter of the type float.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Scalar Type Declaration
<?php
declare(strict_types=1);
function sum(float $a, float $b){ return $a+$b;}
$temp = sum(3,5);
echo "<h1>$temp</h1>";?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Return Type Declaration
As of PHP 7, we can specify the type of the returned
value for the function we declare.
The return type can be string, int, float, bool,
array, callable, self (when defining methods only),
the name of a class or the name of an interface.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Return Type Declaration
When overriding a method, the type of the returned
value of the new defined method must be the same as of
the overridden method.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Return Type Declaration
<?php
declare(strict_types=1);
function sum(int $a, int $b):float{ return $a+$b;}
$temp = sum(sum(5,2),5);
echo "<h1>$temp</h1>";?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Return Type DeclarationLi fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Anonymous Class
As of PHP 7, we can define an anonymous class. It is
highly useful when in a need for one object only.
The new anonymous class can extend another class and
implements as many interfaces as we want.
When defining an anonymous class within the scope of
another class we won't get any access to any of the
private or protected properties of the outer class.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Anonymous Class
<?phpclass C { public function doSomething() { echo "<h1>something</h1>"; }}interface I {}trait T {}$ob = new class(10) extends C implements I { private $num; public function __construct($num) { $this->num = $num; } use T;};$ob->doSomething();?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Anonymous ClassLi fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Unicode
PHP 7 allows us to refer specific characters in the
unicode table.
<?phpecho "\u{0000a9}";echo "\u{00a9}";echo "\u{a9}";?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The Closure call Function
Using this function we can invoke a closure function
on object which isn't the one the closure function is
bounded with.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The Closure call Function
<?phpclass A { private $magic; function __construct($number) { $this->magic = $number; } function getClosure() { return function() { return $this->magic; }; } function setMagic($number) { if($number>0) { $this->magic = $number; } }}
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The Closure call Function
$ob1 = new A(3);$ob2 = new A(4);$ob3 = new A(5);
$f1 = $ob1->getClosure();echo $f1()."<br/>";$ob1->setMagic(7);echo $f1()."<br/>";$f2 = $f1->bindTo($ob2);echo $f1()."<br/>";echo $f2()."<br/>";echo $f1->call($ob3)."<br/>";?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The Closure call FunctionLi fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The unserialize() Function
When we unserialize an object, as of PHP 7 we can
specify the names of the classes that can be
unserialized.
Specifying the names of the classes that can be
unserialized improves the security of our code. When
unserializing untrusted data using this function we
prevent possible code injections.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The unserialize() Function
<?phpclass A { private $magic; function __construct($number) { $this->setMagic($number); } function setMagic($number) { if($number>0) { $this->magic = $number; } } function getMagic() { return $this->magic; }}
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The unserialize() Function
$ob1 = new A(5);$data = serialize($ob1);$ob2 = unserialize(
$data,["allowed_classes" => ["A", "Rectangle"]]);
echo $ob2->getMagic();
?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Grouping use Declarations
PHP 7 allows us to group multiple use declarations in
accordance with the parent namespace.
When grouping together multiple use declarations we get
a clearer code.
We can group together the import of multiple classes,
functions or constants.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Grouping use Declarations
<?php
/*use com\lifemichael\samples\ClassOne;use com\lifemichael\samples\ClassTwo;use com\lifemichael\samples\ClassThree as C;use function com\lifemichael\samples\f1;use function com\lifemichael\samples\f2;use function com\lifemichael\samples\f3;use const com\lifemichael\samples\ConstantA;use const com\lifemichael\samples\ConstantB;use const com\lifemichael\samples\ConstantC;*/
use com\lifemichael\samples\{ClassOne, ClassTwo, ClassThree as C};use function com\lifemichael\samples\{f1, f2, f3};use const com\lifemichael\samples\{ConstantA, ConstantB, ConstantC};?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Generator Return Expression
PHP 7 allows us to include a return statement within a
generator function in order to enable for a final
expression to be returned.
This final expression can be fetched by calling the
getReturn() function on the generator object.
We can call the getReturn() function when the
generator finished yielding its values only.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Generator Return Expression
<?phpfunction numbers() { $sum = 0; for($i=1;$i<=10;$i++) { $sum += $i; yield $i*$i; } return $sum;}$generator = numbers();foreach($generator as $v) { echo "<br/>".$v;}echo "<br/>".$generator->getReturn();?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Generator Return ExpressionLi fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Generator Delegation
When developing generator functions, PHP 7 allows us
to use the yield from <expr> syntax in order to have
the iteration taking place from an <expr> expression
that can be an array or any Traversable object.
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Generator Delegation
<?php
function numbers() { $a = [1,2,3,4]; $b = [10,30,20,60]; yield from $a; yield from $b;}
$generator = numbers();
foreach($generator as $v) { echo "<br/>".$v;}
?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Generator DelegationLi fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
The session_start() Function
As of PHP 7, we can call this function and pass over an
array of options (php.ini options) in order to configure the
way this function works.
session_start(['use_only_cookies' => true]);
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 48
The IIFE Syntax
As of PHP 7 we can define an anonymous function and
immediately invoke it.
({function()})();
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 49
The IIFE Syntax
<?php(function(){ $a = 3; $b = 4; $c = $a + $b; echo "<h1>".$c."</h1>";})();?>
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 50
Invoking Returned Function
As of PHP 7 we can invoke a function we get in return
when calling another function.
doSomething()();
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 51
Invoking Returned Function
<?php
function doSomething() { return function() { echo "<h1>gaga</h1>"; };}
doSomething()();?>
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 52
Exceptions in PHP Engine
As of PHP 7 there are many more new exception types
been used when something goes wrong in the PHP
engine and an exception should be thrown.
TypeError
ParserError
AssertionError
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 53
Exceptions Hierarchy
As of PHP 7 we have a new exceptions hierarchy. We
have the interface Throwable on top, implemented both
by Error and Exception.
The Error and Exception classes are on top of two
separated hierarchies.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 54
Exceptions Hierarchy
This new separation prevents code in PHP 5.x from
catching the new PHP Engine exceptions with catch-all
(catch (Exception $e)) clauses.
When creating a new exception class we should extend
one of the pre defined classes. We cannot implement
Throwable directly.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 55
Integers
As of PHP 7, casting NAN and INF into integers will
result in 0. Prior to PHP 7 we would have received a very
long number.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 56
Integers
<?php$a = NAN;$b = (int)$a;echo "<h1>".$b."</h1>";$a = INF;$b = (int)$a;echo "<h1>".$b."</h1>";?>
PHP 7PHP 5.x
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 57
The JSON Extension
As of PHP 7, due to legal issues the JSON extension
was replaced with a new one. The JSOND extension.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 58
The foreach Loop Fixes
PHP 7 introduces fixes into the foreach loop in order to
ensure the same behavior in all PHP implementations.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 59
The list Construct Changes
As of PHP 7 we cannot use the list construct with
strings. This limitation was introduced in order to have
the same behavior in all PHP engines. Prior to PHP 7, in
some cases it was possible to use the list construct
together with strings.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 60
Division By Zero Changes
Before PHP 7, when dividing by 0 or calculating modulo
by 0 we got the value false of the type boolean.
As of PHP 7, when calculating the modulo by 0 the
DivisionByZeroError exception will be thrown and when
trying to divide by 0 we will get +INF, -INF or NAN.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 61
Division By Zero Changes
<?php$a = -512;$b = 0;$temp1 = $a / $b;echo "<h1>temp1=".$temp1." ".gettype($temp1)."</h1>";try{ $temp2 = $a % $b; echo "<h1>temp2=" . $temp2 . " " . gettype($temp2) . "</h1>";}catch(Throwable $throwable){ echo "<h1>error happened</h1>";}?>
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 62
Division By Zero ChangesLi fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 63
PHP4 Constructors Deprecation
As of PHP 7, the constructors we define must be named
with the '__construct' name. We should avoid naming
our constructors after the classes in which we define
them.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 64
The date.timezone Warning
As of PHP 7, we will no longer get the popular
date.timezone warning. So far, in order to remove
that warning we had to have access to PHP.ini. As of
PHP 7 things will be simpler.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 65
The PHP Alternative Tags Removal
As of PHP 7, the alternative tags <% (and <%=), %>,
<script language="php"> (and </script>) are
no longer supported.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 66
Multiple default Blocks in Switch
As of PHP 7, it isn't possible to have multiple default
blocks in a switch statement.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 67
Functions Multiple Parameters Names
As of PHP 7, it is no longer possible to define a function
with two or more parameters with the same name.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 68
Server APIs Removal
As of PHP 7, lots of server APIs are no longer part of the
core PHP engine. sapi/apache
sapi/apache_hooks
sapi/thttpd
...
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 69
Numerical Strings Hex Support
As of PHP 7, strings we create that include hexadecimal
numbers are no longer recognized as numerical.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 70
Deprecated Functionality
As with the release of PHP 7, many of the deprecated
functionality we know in PHP has been removed.
One of extensions that was removed is the mysql
extension that allows us to use MySQL in a procedural
way.
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 71
The IIFE Syntax
<?php(function(){ $a = 3; $b = 4; $c = $a + $b; echo "<h1>".$c."</h1>";})();?>
Li fe M
ic hae l .c o
m
IIFE stands for Immediately Invoked Function Expression
09/22/16 © Haim Michael 2011. All Rights Reserved. 72
Invoking Returned Function
As of PHP 7 we can invoke a function we get in return
when calling another function.
doSomething()();
Li fe M
ic hae l .c o
m
09/22/16 © Haim Michael 2011. All Rights Reserved. 73
Invoking Returned Function
<?php
function doSomething() { return function() { echo "<h1>gaga</h1>"; };}
doSomething()();?>
Li fe M
ic hae l .c o
m
© Haim Michael 2011. All Rights Reserved.
Questions & Answers● If you enjoyed my lecture please leave me a comment
at http://speakerpedia.com/speakers/life-michael.
Thanks for your time!
Haim.
Li fe M
ic hae l .c o
m
top related