© Haim Michael 2011. All Rights Reserved.
PHP7. Game Changer.
Haim Michael
September 22nd
, 2016
All logos, trade marks and brand names used in this presentation belong
to the respective owners.
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Haim Michael Introduction
● Snowboarding. Learning. Coding. Teaching. More than
16 years of Practical Experience.
LifeMichael.com
© 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
LifeMichael.com
© 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.
LifeMichael.com
© 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.
LifeMichael.com
© 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.
LifeMichael.com
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.
LifeMichael.com
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%.
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Backward Compatibility
 Some of the changes introduced by PHP 7 cause
backward compatibility problems.
LifeMichael.com
© 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.
LifeMichael.com
© 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>";
?>
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
The <=> Operator
LifeMichael.com
© 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.
LifeMichael.com
© 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.
LifeMichael.com
© 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>";
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
The ?? Operator
LifeMichael.com
© 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.
LifeMichael.com
© 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.
LifeMichael.com
© 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.
LifeMichael.com
© 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>";
?>
LifeMichael.com
© 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.
LifeMichael.com
© 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.
LifeMichael.com
© 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>";
?>
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Return Type Declaration
LifeMichael.com
© 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.
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Anonymous Class
<?php
class 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();
?>
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Anonymous Class
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Unicode
 PHP 7 allows us to refer specific characters in the
unicode table.
<?php
echo "u{0000a9}";
echo "u{00a9}";
echo "u{a9}";
?>
LifeMichael.com
© 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.
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
The Closure call Function
<?php
class 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;
}
}
}
LifeMichael.com
© 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/>";
?>
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
The Closure call Function
LifeMichael.com
© 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.
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
The unserialize() Function
<?php
class A {
private $magic;
function __construct($number) {
$this->setMagic($number);
}
function setMagic($number) {
if($number>0) {
$this->magic = $number;
}
}
function getMagic() {
return $this->magic;
}
}
LifeMichael.com
© 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();
?>
LifeMichael.com
© 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.
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Grouping use Declarations
<?php
/*
use comlifemichaelsamplesClassOne;
use comlifemichaelsamplesClassTwo;
use comlifemichaelsamplesClassThree as C;
use function comlifemichaelsamplesf1;
use function comlifemichaelsamplesf2;
use function comlifemichaelsamplesf3;
use const comlifemichaelsamplesConstantA;
use const comlifemichaelsamplesConstantB;
use const comlifemichaelsamplesConstantC;
*/
use comlifemichaelsamples{ClassOne, ClassTwo, ClassThree as C};
use function comlifemichaelsamples{f1, f2, f3};
use const comlifemichaelsamples{ConstantA, ConstantB, ConstantC};
?>
LifeMichael.com
© 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.
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Generator Return Expression
<?php
function 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();
?>
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Generator Return Expression
LifeMichael.com
© 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.
LifeMichael.com
© 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;
}
?>
LifeMichael.com
© Haim Michael 2011. All Rights Reserved.
Generator Delegation
LifeMichael.com
© 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]);
LifeMichael.com
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()})();
LifeMichael.com
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>";
})();
?>
LifeMichael.com
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()();
LifeMichael.com
09/22/16 © Haim Michael 2011. All Rights Reserved. 51
Invoking Returned Function
<?php
function doSomething() {
return function() {
echo "<h1>gaga</h1>";
};
}
doSomething()();
?>
LifeMichael.com
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
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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>";
}
?>
LifeMichael.com
09/22/16 © Haim Michael 2011. All Rights Reserved. 62
Division By Zero Changes
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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
...
LifeMichael.com
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.
LifeMichael.com
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.
LifeMichael.com
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>";
})();
?>
LifeMichael.com
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()();
LifeMichael.com
09/22/16 © Haim Michael 2011. All Rights Reserved. 73
Invoking Returned Function
<?php
function doSomething() {
return function() {
echo "<h1>gaga</h1>";
};
}
doSomething()();
?>
LifeMichael.com
© 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.
LifeMichael.com

PHP7. Game Changer.

  • 1.
    © Haim Michael2011. All Rights Reserved. PHP7. Game Changer. Haim Michael September 22nd , 2016 All logos, trade marks and brand names used in this presentation belong to the respective owners. LifeMichael.com
  • 2.
    © Haim Michael2011. All Rights Reserved. Haim Michael Introduction ● Snowboarding. Learning. Coding. Teaching. More than 16 years of Practical Experience. LifeMichael.com
  • 3.
    © Haim Michael2011. 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 LifeMichael.com
  • 4.
    © Haim Michael2011. 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. LifeMichael.com
  • 5.
    © Haim Michael2011. 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. LifeMichael.com
  • 6.
    © Haim Michael2011. 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. LifeMichael.com
  • 7.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 8.
    09/22/16 © HaimMichael 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%. LifeMichael.com
  • 9.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 10.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 11.
    09/22/16 © HaimMichael 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 LifeMichael.com
  • 12.
    © Haim Michael2011. All Rights Reserved. Backward Compatibility  Some of the changes introduced by PHP 7 cause backward compatibility problems. LifeMichael.com
  • 13.
    © Haim Michael2011. 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. LifeMichael.com
  • 14.
    © Haim Michael2011. 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>"; ?> LifeMichael.com
  • 15.
    © Haim Michael2011. All Rights Reserved. The <=> Operator LifeMichael.com
  • 16.
    © Haim Michael2011. 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. LifeMichael.com
  • 17.
    © Haim Michael2011. 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. LifeMichael.com
  • 18.
    © Haim Michael2011. 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>"; LifeMichael.com
  • 19.
    © Haim Michael2011. All Rights Reserved. The ?? Operator LifeMichael.com
  • 20.
    © Haim Michael2011. 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. LifeMichael.com
  • 21.
    © Haim Michael2011. 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. LifeMichael.com
  • 22.
    © Haim Michael2011. 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. LifeMichael.com
  • 23.
    © Haim Michael2011. 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>"; ?> LifeMichael.com
  • 24.
    © Haim Michael2011. 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. LifeMichael.com
  • 25.
    © Haim Michael2011. 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. LifeMichael.com
  • 26.
    © Haim Michael2011. 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>"; ?> LifeMichael.com
  • 27.
    © Haim Michael2011. All Rights Reserved. Return Type Declaration LifeMichael.com
  • 28.
    © Haim Michael2011. 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. LifeMichael.com
  • 29.
    © Haim Michael2011. All Rights Reserved. Anonymous Class <?php class 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(); ?> LifeMichael.com
  • 30.
    © Haim Michael2011. All Rights Reserved. Anonymous Class LifeMichael.com
  • 31.
    © Haim Michael2011. All Rights Reserved. Unicode  PHP 7 allows us to refer specific characters in the unicode table. <?php echo "u{0000a9}"; echo "u{00a9}"; echo "u{a9}"; ?> LifeMichael.com
  • 32.
    © Haim Michael2011. 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. LifeMichael.com
  • 33.
    © Haim Michael2011. All Rights Reserved. The Closure call Function <?php class 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; } } } LifeMichael.com
  • 34.
    © Haim Michael2011. 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/>"; ?> LifeMichael.com
  • 35.
    © Haim Michael2011. All Rights Reserved. The Closure call Function LifeMichael.com
  • 36.
    © Haim Michael2011. 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. LifeMichael.com
  • 37.
    © Haim Michael2011. All Rights Reserved. The unserialize() Function <?php class A { private $magic; function __construct($number) { $this->setMagic($number); } function setMagic($number) { if($number>0) { $this->magic = $number; } } function getMagic() { return $this->magic; } } LifeMichael.com
  • 38.
    © Haim Michael2011. All Rights Reserved. The unserialize() Function $ob1 = new A(5); $data = serialize($ob1); $ob2 = unserialize( $data, ["allowed_classes" => ["A", "Rectangle"]]); echo $ob2->getMagic(); ?> LifeMichael.com
  • 39.
    © Haim Michael2011. 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. LifeMichael.com
  • 40.
    © Haim Michael2011. All Rights Reserved. Grouping use Declarations <?php /* use comlifemichaelsamplesClassOne; use comlifemichaelsamplesClassTwo; use comlifemichaelsamplesClassThree as C; use function comlifemichaelsamplesf1; use function comlifemichaelsamplesf2; use function comlifemichaelsamplesf3; use const comlifemichaelsamplesConstantA; use const comlifemichaelsamplesConstantB; use const comlifemichaelsamplesConstantC; */ use comlifemichaelsamples{ClassOne, ClassTwo, ClassThree as C}; use function comlifemichaelsamples{f1, f2, f3}; use const comlifemichaelsamples{ConstantA, ConstantB, ConstantC}; ?> LifeMichael.com
  • 41.
    © Haim Michael2011. 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. LifeMichael.com
  • 42.
    © Haim Michael2011. All Rights Reserved. Generator Return Expression <?php function 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(); ?> LifeMichael.com
  • 43.
    © Haim Michael2011. All Rights Reserved. Generator Return Expression LifeMichael.com
  • 44.
    © Haim Michael2011. 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. LifeMichael.com
  • 45.
    © Haim Michael2011. 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; } ?> LifeMichael.com
  • 46.
    © Haim Michael2011. All Rights Reserved. Generator Delegation LifeMichael.com
  • 47.
    © Haim Michael2011. 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]); LifeMichael.com
  • 48.
    09/22/16 © HaimMichael 2011. All Rights Reserved. 48 The IIFE Syntax  As of PHP 7 we can define an anonymous function and immediately invoke it. ({function()})(); LifeMichael.com
  • 49.
    09/22/16 © HaimMichael 2011. All Rights Reserved. 49 The IIFE Syntax <?php (function(){ $a = 3; $b = 4; $c = $a + $b; echo "<h1>".$c."</h1>"; })(); ?> LifeMichael.com
  • 50.
    09/22/16 © HaimMichael 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()(); LifeMichael.com
  • 51.
    09/22/16 © HaimMichael 2011. All Rights Reserved. 51 Invoking Returned Function <?php function doSomething() { return function() { echo "<h1>gaga</h1>"; }; } doSomething()(); ?> LifeMichael.com
  • 52.
    09/22/16 © HaimMichael 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 LifeMichael.com
  • 53.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 54.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 55.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 56.
    09/22/16 © HaimMichael 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 LifeMichael.com
  • 57.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 58.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 59.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 60.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 61.
    09/22/16 © HaimMichael 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>"; } ?> LifeMichael.com
  • 62.
    09/22/16 © HaimMichael 2011. All Rights Reserved. 62 Division By Zero Changes LifeMichael.com
  • 63.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 64.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 65.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 66.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 67.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 68.
    09/22/16 © HaimMichael 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 ... LifeMichael.com
  • 69.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 70.
    09/22/16 © HaimMichael 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. LifeMichael.com
  • 71.
    09/22/16 © HaimMichael 2011. All Rights Reserved. 71 The IIFE Syntax <?php (function(){ $a = 3; $b = 4; $c = $a + $b; echo "<h1>".$c."</h1>"; })(); ?> LifeMichael.com IIFE stands for Immediately Invoked Function Expression
  • 72.
    09/22/16 © HaimMichael 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()(); LifeMichael.com
  • 73.
    09/22/16 © HaimMichael 2011. All Rights Reserved. 73 Invoking Returned Function <?php function doSomething() { return function() { echo "<h1>gaga</h1>"; }; } doSomething()(); ?> LifeMichael.com
  • 74.
    © Haim Michael2011. 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. LifeMichael.com