Slide 1

Slide 1 text

new features in php7 By Jeremy Lindblom @jeremeamia McGraw-Hill Education

Slide 2

Slide 2 text

@jeremeamia

Slide 3

Slide 3 text

today: 1. brief history 2.what’s new? 3.what changed?

Slide 4

Slide 4 text

did you know... php7 is compatible with star wars & star trek fans?

Slide 5

Slide 5 text

new features or maybe... an awakening of new powers? php7 a.k.a. PHP VII

Slide 6

Slide 6 text

PHP VII: The PHorce Awakens

Slide 7

Slide 7 text

PHP7 is based on a new version or the “next generation” of the Zend Engine, called: phpng

Slide 8

Slide 8 text

PHP: The Next Generation

Slide 9

Slide 9 text

a brief history of php part one

Slide 10

Slide 10 text

Once upon a time…

Slide 11

Slide 11 text

1995: PHP is born

Slide 12

Slide 12 text

1998: PHP 3 & The ElePHPant

Slide 13

Slide 13 text

2000: PHP 4 & Zend Engine

Slide 14

Slide 14 text

2003: ???

Slide 15

Slide 15 text

2003: Jeremy Graduates

Slide 16

Slide 16 text

2003: WordPress

Slide 17

Slide 17 text

2004: PHP 5 & OOP

Slide 18

Slide 18 text

2006: AWS & The Cloud

Slide 19

Slide 19 text

2007: PHP 5.2 & Frameworks

Slide 20

Slide 20 text

2009: PHP 5.3 is released

Slide 21

Slide 21 text

2010: ♥ for Git/GitHub grows

Slide 22

Slide 22 text

2011: Composer & PSR-0

Slide 23

Slide 23 text

2013: Laravel, hhvm, & PHP 5.5

Slide 24

Slide 24 text

2015: PHP7 & 20th Birthday

Slide 25

Slide 25 text

2016: PHP 7.1 & Beyond

Slide 26

Slide 26 text

what’s new in php7? part two

Slide 27

Slide 27 text

But first...

Slide 28

Slide 28 text

New Features in PHP 5 Since Version 5.2 Namespaces & Class Imports Late Static Binding Closures goto Ternary Shortcut ( ?: ) Traits Short Array Syntax ( [ ] ) Built-in Server Generators password_hash() API finally Variadic Functions ( ... ) Function/Const Imports Exponentiation Operator ( ** )

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

It’s faster!

Slide 31

Slide 31 text

Like, waaay faster!!!

Slide 32

Slide 32 text

Benchmark: Wordpress 4.1

Slide 33

Slide 33 text

Benchmark: Wordpress 4.1

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

➡ Twice as fast ➡ Half the memory

Slide 36

Slide 36 text

Your Server Cluster

Slide 37

Slide 37 text

Your Server Cluster

Slide 38

Slide 38 text

➡ Twice as fast ➡ Half the memory ∴ Lower Costs

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

Scalar Type Hints

Slide 41

Slide 41 text

Type hints in PHP 5 // “array”, “callable”, “self”, and class names allowed function getPeople(array $ids) { … } function delete($id, callable $onFailure) { … } function hire(Person $person) { … }

Slide 42

Slide 42 text

Type declarations in PHP 7 (a.k.a. scalar type hints) // “int”, “float”, “string”, and “bool” now allowed function truncate(string $str, int $length) { … } function coord(float $x, float $y) { … }

Slide 43

Slide 43 text

Coercive vs. Strict // Given the following function: function sum(int ...$numbers) { return array_sum($numbers); } echo sum(3, 1, 7); //> 11

Slide 44

Slide 44 text

Coercive vs. Strict (cont.) declare(strict_types=0); // coercive mode echo sum(4, 2); //> 6 echo sum(4.1, 2.1); //> 6

Slide 45

Slide 45 text

Coercive vs. Strict (cont.) declare(strict_types=1); // strict mode echo sum(4, 2); //> 6 echo sum(4.1, 2.1); //> TypeError

Slide 46

Slide 46 text

[PHP 7.1] Nullable Type Hints function prefix(?string $prefix, string $str) { … }

Slide 47

Slide 47 text

Return Type Declarations

Slide 48

Slide 48 text

Works with functions, methods, and closures function sum(int ...$numbers): int { … } $fn = function (int $num) use ($max): int { … }

Slide 49

Slide 49 text

[PHP 7.1] Void Return Types function execute(Command $cmd): void { … }

Slide 50

Slide 50 text

Null Coalescing Operator

Slide 51

Slide 51 text

Ternary Statements in PHP 5 // Ternary with set variable $result = !empty($value) ? $value : $default; // Null coalescing with non-set variable $result = isset($value) ? $value : $default;

Slide 52

Slide 52 text

Ternary Shortcut Syntax (Added in PHP 5.3) // Ternary with set variable $result = $value ?: $default; // Null coalescing with non-set variable $result = isset($value) ? $value : $default;

Slide 53

Slide 53 text

Null Coalescing Operator (Added in PHP 7) // Ternary with set variable $result = $value ?: $default; // Null coalescing with non-set variable $result = $value ?? $default;

Slide 54

Slide 54 text

Anonymous Classes

Slide 55

Slide 55 text

Anonymous Classes use Psr\Log\{LoggerInterface, LoggerTrait}; $log = new class() implements LoggerInterface { use LoggerTrait; public function log($lvl, $msg, array $ctx = []) { echo $lvl . ': ' . strtr($msg, $ctx) . "\n"; } };

Slide 56

Slide 56 text

Anonymous Classes and Constructors $log = new class(string $path) implements LoggerInterface { use LoggerTrait; private $file; public function __construct(string $path) { $this->file = fopen($path, 'w+'); } };

Slide 57

Slide 57 text

Anonymous Classes Tips ● function is to closure as class is to anonymous class ● Works the same as a regular class, but without a name ● May be helpful for implementing... ○ internal, private, or undocumented classes ○ single-use objects ○ implementing small interfaces ● Cannot be serialized

Slide 58

Slide 58 text

Group use Declarations

Slide 59

Slide 59 text

Group use Declarations use Psr\Log\{LoggerInterface, LoggerTrait}; $log = new class() implements LoggerInterface { use LoggerTrait; public function log($lvl, $msg, array $ctx = []) { echo $lvl . ': ' . strtr($msg, $ctx) . "\n"; } };

Slide 60

Slide 60 text

More Examples – PHP 5 use My\App\ClassA; use My\App\ClassB as B; use function My\App\function_a; use function My\App\function_b; use const My\App\CONSTANT_A; use const My\App\CONSTANT_B;

Slide 61

Slide 61 text

More Examples – PHP 7 use My\App\{ClassA, ClassB as B}; use function My\App\{function_a, function_b}; use const My\App\{CONSTANT_A, CONSTANT_B};

Slide 62

Slide 62 text

Spaceship Operator <=> <=>

Slide 63

Slide 63 text

Combined Comparison (“Spaceship”) Operator ● Looks like a UFO: $a <=> $b ● Like strcmp() but for numbers, other scalars, and arrays ● Returns -1, 0, or 1, which is perfect for sorting ● Same logic can be accomplished for numbers by doing: ($a < $b) ? -1 : (($a > $b) ? 1 : 0)

Slide 64

Slide 64 text

Spaceship in Action usort($people, function ($person1, $person2) { return $person1->age <=> $person2->age; });

Slide 65

Slide 65 text

CSPRNG Functions

Slide 66

Slide 66 text

CSPRNG Functions Cryptographically Secure Pseudo– Random Number Generator random_int($min, $max): int random_bytes($length): string

Slide 67

Slide 67 text

Symmetric Array Destructuring PHP 7.1

Slide 68

Slide 68 text

[PHP 7.1] Symmetric Array Destructuring // Destructuring in PHP 5 list($class, $method) = explode('::', $name); // Say goodbye to list() in PHP 7 [$class, $method] = explode('::', $name); // Can also destructure arrays with non-integer keys ['id' => $id, 'name' => $name] = $data;

Slide 69

Slide 69 text

Class Constant Visibility PHP 7.1

Slide 70

Slide 70 text

[PHP 7.1] Class Constant Visibility class ConstDemo { public const PUBLIC_CONST_B = 1; protected const PROTECTED_CONST = 2; private const PRIVATE_CONST = 4; }

Slide 71

Slide 71 text

Generator Return Expressions Generator Delegation ( yield from ) ReflectionGenerator Class Unicode Escape Syntax IntlChar Class Filtered unserialize() Multi-catch Exception Handling iterable pseudo-type Expectations via assert() intdiv() Function New session_start() Options Constant Arrays via define() Closure::call() Method Multi-level dirname() w/ New Arg Negative string offsets ( $str[-2]) Closure::fromCallable() Method Additional New Features in PHP 7.x 7.1

Slide 72

Slide 72 text

what has been changed? part three

Slide 73

Slide 73 text

i.e., what has been broken ? part three

Slide 74

Slide 74 text

Throwing New Things

Slide 75

Slide 75 text

Exception RuntimeException LogicException PHP 5 Exception Hierarchy

Slide 76

Slide 76 text

Error TypeError ParseError ArithmeticError PHP 7 Error Hierarchy

Slide 77

Slide 77 text

Error TypeError ParseError ArithmeticError PHP 7 Throwable Top-Level Interface Throwable Exception RuntimeException LogicException

Slide 78

Slide 78 text

Exceptions, Errors, and Throwables ● Fatal errors are now a type of exception, or Throwable ● To catch anything, catch Throwable ● Be careful with functions type hinting for Exception, because they may end up receiving a Throwable instead ● Parsing errors when using eval() now throw ParseError ● Dividing by zero now throws a DivisionByZeroError

Slide 79

Slide 79 text

More Consistent Syntax

Slide 80

Slide 80 text

Uniform Variable Syntax ● PHP uses an AST under-the-hood now ● Better consistency at the cost of minor backward-incompatibility issues ● Handling of variables/functions is left-to-right always ● When in doubt, add { } or ( )

Slide 81

Slide 81 text

Uniform Variable Syntax – Examples

Slide 82

Slide 82 text

Forbid Dynamic Scope Funcs PHP 7.1

Slide 83

Slide 83 text

[PHP 7.1] Forbid Dynamic Scope Functions Can no longer dynamically call the following functions: assert() compact() extract() func_get_args() func_get_arg() func_num_args() get_defined_vars() mb_parse_str() parse_str()

Slide 84

Slide 84 text

Not All Extensions Available

Slide 85

Slide 85 text

GoPHP7-ext http://gophp7.org/gophp7-ext/

Slide 86

Slide 86 text

Removed the Rubbish

Slide 87

Slide 87 text

Removed and/or Deprecated ● mysql, mssql, and ereg extensions are gone (Use mysqli/pdo, and pcre like you’ve already been doing) ● PHP 4 style constructors deprecated class Foo { function Foo($bar) { … } } ● Can no longer assign-by-reference with new $foo =& new Foo(); ● Can no longer repeat parameter names function foo($fizz, $_, $_, $buzz) { … }

Slide 88

Slide 88 text

Removed and/or Deprecated (cont.) ● Static calls to non-static methods deprecated class Foo { function bar() { … } } Foo::bar(); ● No more E_STRICT errors. ● No more alternative PHP tags ( <%, <%=, or ) ● Can no longer have multiple default blocks in a switch (They were ignored before, now they cause an error)

Slide 89

Slide 89 text

more resources appendix

Slide 90

Slide 90 text

Upgrading to PHP 7 ● Upgrade to PHP 5.6, if you aren’t there yet ● Review the migration guides and your code ● For libraries, use travis-ci.org to run your tests on PHP 7 ● Use 3v4l.org to try out code snippets ● Use “The Cloud”, Docker, and/or Vagrant for testing ● Homebrew has PHP 7

Slide 91

Slide 91 text

Migration Guide http://php.net/manual/en/migration70.php

Slide 92

Slide 92 text

Thanks! By Jeremy Lindblom @jeremeamia McGraw-Hill Education