Slide 1

Slide 1 text

P H P 7 . N E X T : T H E N E W B I T S

Slide 2

Slide 2 text

D A V E Y S H A F I K • Developer • Author • Open Source Contributor • @dshafik

Slide 3

Slide 3 text

h t t p s : / / d e v e l o p e r . a k a m a i . c o m

Slide 4

Slide 4 text

Let’s start a conversation about mental health in tech mhprompt.org

Slide 5

Slide 5 text

R E L E A S E M A N A G E R

Slide 6

Slide 6 text

P H P I S D E A D ? CC-BY 2.0: Nana B Agyei

Slide 7

Slide 7 text

W H A T D O E S P R O G R E S S L O O K L I K E ? CC-BY 2.0: damon jah

Slide 8

Slide 8 text

C U R R E N T U S A G E

Slide 9

Slide 9 text

P H P U S A G E Source: w3techs

Slide 10

Slide 10 text

P H P V E R S I O N S Source: composer (seld.be)

Slide 11

Slide 11 text

P H P M A J O R V E R S I O N S 5.x 46% 7.x 54%

Slide 12

Slide 12 text

P H P I S E V O L V I N G CC-BY 2.0: Duncan Hull

Slide 13

Slide 13 text

S H O R T E C H O T A G S P H P 5 . 4

Slide 14

Slide 14 text

B I N A R Y N U M B E R S P H P 5 . 4

Slide 15

Slide 15 text

S H O R T A R R A Y S Y N T A X P H P 5 . 4

Slide 16

Slide 16 text

A R R A Y D E R E F E R E N C I N G P H P 5 . 4

Slide 17

Slide 17 text

C L O S U R E $ T H I S B I N D I N G P H P 5 . 4

Slide 18

Slide 18 text

C A L L A B L E T Y P E H I N T P H P 5 . 4

Slide 19

Slide 19 text

T R A I T S P H P 5 . 4

Slide 20

Slide 20 text

B U I L T I N C L I S E R V E R P H P 5 . 4

Slide 21

Slide 21 text

L I S T S U P P O R T I N F O R E A C H P H P 5 . 5

Slide 22

Slide 22 text

A R B I T R A R Y E X P R E S S I O N S U P P O R T F O R E M P T Y ( ) P H P 5 . 5

Slide 23

Slide 23 text

S T R I N G / A R R A Y D E R E F E R E N C I N G P H P 5 . 5

Slide 24

Slide 24 text

F I N A L L Y B L O C K S P H P 5 . 5

Slide 25

Slide 25 text

E X T / P A S S W O R D P H P 5 . 5

Slide 26

Slide 26 text

G E N E R A T O R S P H P 5 . 5

Slide 27

Slide 27 text

L A N G U A G E S P E C I F I C A T I O N T H A N K S F A C E B O O K !

Slide 28

Slide 28 text

E X P O N E N T O P E R A T O R ( T _ P O W ) P H P 5 . 6

Slide 29

Slide 29 text

C O N S T A N T S C A L A R E X P R E S S I O N S P H P 5 . 6

Slide 30

Slide 30 text

P H P D B G P H P 5 . 6

Slide 31

Slide 31 text

I M P O R T F U N C T I O N S / C O N S T A N T S P H P 5 . 6

Slide 32

Slide 32 text

V A R I A D I C S P H P 5 . 6

Slide 33

Slide 33 text

A R G U M E N T U N P A C K I N G ( S P L A T ) P H P 5 . 6

Slide 34

Slide 34 text

R E M O V E A L T E R N A T I V E P H P T A G S P H P 7 . 0

Slide 35

Slide 35 text

R E M O V E P O S I X R E G E X P H P 7 . 0

Slide 36

Slide 36 text

R E M O V E E X T / M Y S Q L P H P 7 . 0

Slide 37

Slide 37 text

2 . 4 X F A S T E R P H P 7 . 0

Slide 38

Slide 38 text

A B S T R A C T S Y N T A X T R E E P H P 7 . 0

Slide 39

Slide 39 text

U N I F O R M V A R I A B L E S Y N T A X P H P 7 . 0

Slide 40

Slide 40 text

E N G I N E E X C E P T I O N S P H P 7 . 0

Slide 41

Slide 41 text

U N I C O D E E S C A P E S Y N T A X P H P 7 . 0

Slide 42

Slide 42 text

N U L L C O A L E S C E O P E R A T O R P H P 7 . 0

Slide 43

Slide 43 text

B I N D C L O S U R E O N C A L L P H P 7 . 0

Slide 44

Slide 44 text

G R O U P U S E D E C L A R A T I O N S P H P 7 . 0

Slide 45

Slide 45 text

G E N E R A T O R D E L E G A T I O N P H P 7 . 0

Slide 46

Slide 46 text

G E N E R A T O R R E T U R N V A L U E S P H P 7 . 0

Slide 47

Slide 47 text

A N O N Y M O U S C L A S S E S P H P 7 . 0

Slide 48

Slide 48 text

S C A L A R T Y P E H I N T S P H P 7 . 0

Slide 49

Slide 49 text

R E T U R N H I N T S P H P 7 . 0

Slide 50

Slide 50 text

S T R I C T T Y P E S P H P 7 . 0

Slide 51

Slide 51 text

S P A C E S H I P O P E R A T O R P H P 7 . 0

Slide 52

Slide 52 text

C O M B I N E D C O M P A R I S O N O P E R A T O R P H P 7 . 0

Slide 53

Slide 53 text

P R E S E N T D A Y Credit: NASA/KSC

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

T O O F E W A R G U M E N T S E X C E P T I O N

Slide 56

Slide 56 text

T O O F E W A R G U M E N T S E X C E P T I O N • Throw an exception when too few arguments are passed to a user defined function/method regardless of strict types • Backwards compatibility break

Slide 57

Slide 57 text

function hello($whom) { echo "Hello $whom"; } hello(); 1 2 3 4 5 6 T O O F E W A R G U M E N T S E X C E P T I O N

Slide 58

Slide 58 text

function hello($whom) { echo "Hello $whom"; } hello(); 1 2 3 4 5 6 T O O F E W A R G U M E N T S E X C E P T I O N

Slide 59

Slide 59 text

function hello($whom) { echo "Hello $whom"; } hello(); 1 2 3 4 5 6 T O O F E W A R G U M E N T S E X C E P T I O N

Slide 60

Slide 60 text

function hello($whom) { echo "Hello $whom"; } hello(); 1 2 3 4 5 6 T O O F E W A R G U M E N T S E X C E P T I O N

Slide 61

Slide 61 text

function hello($whom) { echo "Hello $whom"; } hello(); 1 2 3 4 5 6 T O O F E W A R G U M E N T S E X C E P T I O N Warning: Missing argument 1 for hello(), called in on line 6 and defined in on line 1

Slide 62

Slide 62 text

function hello($whom) { echo "Hello $whom"; } hello(); 1 2 3 4 5 6 T O O F E W A R G U M E N T S E X C E P T I O N Notice: Undefined variable: what in on line 4 Hello

Slide 63

Slide 63 text

T O O F E W A R G U M E N T S E X C E P T I O N 1 2 3 4 5 6 function hello($whom) { echo "Hello $whom"; } hello();

Slide 64

Slide 64 text

T O O F E W A R G U M E N T S E X C E P T I O N function hello($whom) { echo "Hello $whom"; } hello(); 1 2 3 4 5 6 Fatal error: Uncaught ArgumentCountError: Too few arguments to function hello(), 0 passed in on line 6 and exactly 1 expected in :1

Slide 65

Slide 65 text

T O O F E W A R G U M E N T S E X C E P T I O N function hello($whom) { echo "Hello $whom"; } try { hello(); } catch (\ArgumentCountError $e) { // Handle exception } 1 2 3 4 5 6 7 8 9 10

Slide 66

Slide 66 text

T O O F E W A R G U M E N T S E X C E P T I O N function hello($whom) { echo "Hello $whom"; } try { hello(); } catch (\ArgumentCountError $e) { // Handle exception } 1 2 3 4 5 6 7 8 9 10

Slide 67

Slide 67 text

T O O F E W A R G U M E N T S E X C E P T I O N function hello($whom, $when) { echo "Hello $whom, have a good $when"; } try { $args = ['World']; hello(... $args); } catch (\ArgumentCountError $e) { // Handle exception } 1 2 3 4 5 6 7 8 9 10 11

Slide 68

Slide 68 text

T O O F E W A R G U M E N T S E X C E P T I O N function hello($whom, $when) { echo "Hello $whom, have a good $when"; } try { $args = ['World']; hello(... $args); } catch (\ArgumentCountError $e) { // Handle exception } 1 2 3 4 5 6 7 8 9 10 11

Slide 69

Slide 69 text

E X C E P T I O N H I E R A R C H Y \Throwable interface ├── \Exception implements \Throwable └── \Error implements \Throwable ├── \TypeError extends \Error ├── \ParseError extends \Error └── \AssertionError extends \Error

Slide 70

Slide 70 text

E X C E P T I O N H I E R A R C H Y \Throwable interface ├── \Exception implements \Throwable └── \Error implements \Throwable ├── \TypeError extends \Error │ └── \ArgumentCountError extends \TypeError ├── \ParseError extends \Error └── \AssertionError extends \Error

Slide 71

Slide 71 text

N E G A T I V E S T R I N G O F F S E T S

Slide 72

Slide 72 text

N E G A T I V E S T R I N G O F F S E T S • Consistently support negative string offsets • Similar to substr() • In string character accessors • Internal functions

Slide 73

Slide 73 text

N E G A T I V E S T R I N G O F F S E T S $path = "/path/to/somewhere/"; if (substr($path, -1) !== '/') { $path = substr($path, 0, -1); }

Slide 74

Slide 74 text

N E G A T I V E S T R I N G O F F S E T S $path = "/path/to/somewhere/"; if ($path[-1] === '/') { $path = substr($path, 0, -1); }

Slide 75

Slide 75 text

N E G A T I V E S T R I N G O F F S E T S $path = "/path/to/somewhere/"; if ($path{-1} === '/') { $path = substr($path, 0, -1); }

Slide 76

Slide 76 text

S E T C H A R A C T E R S $string = "abc"; $string[-2] = "z"; // $string == "azc"

Slide 77

Slide 77 text

C H E C K L E N G T H if (strlen($_POST['password']) < 8) { // Do something } if (!isset($_POST['password'][7])) { // Do something } if (!isset($_POST['password'][-8])) { // Do something }

Slide 78

Slide 78 text

if (strlen($_POST['password']) < 8) { // Do something } if (!isset($_POST['password'][7])) { // Do something } if (!isset($_POST['password'][-8])) { // Do something } C H E C K L E N G T H

Slide 79

Slide 79 text

if (strlen($_POST['password']) < 8) { // Do something } if (!isset($_POST['password'][7])) { // Do something } if (!isset($_POST['password'][-8])) { // Do something } C H E C K L E N G T H

Slide 80

Slide 80 text

F U N C T I O N S U P P O R T strpos() stripos() substr_count() grapheme_strpos() grapheme_stripos() grapheme_extract() iconv_strpos() file_get_contents() mb_strimwidth() mb_ereg_search_setpos() mb_strpos() mb_stripos()

Slide 81

Slide 81 text

C L O S U R E F R O M C A L L A B L E

Slide 82

Slide 82 text

C L O S U R E F R O M C A L L A B L E class Closure { public static function fromCallable(callable $callable): Closure { ... } }

Slide 83

Slide 83 text

C L O S I N G O V E R S C O P E

Slide 84

Slide 84 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 85

Slide 85 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 86

Slide 86 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 87

Slide 87 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 88

Slide 88 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 89

Slide 89 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 90

Slide 90 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 91

Slide 91 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 92

Slide 92 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 93

Slide 93 text

C L O S I N G O V E R S C O P E Fatal error: Uncaught Error: Call to private method Validator::emailValidation() from context '' in :

Slide 94

Slide 94 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 95

Slide 95 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } public function emailValidation($userData) {...} public function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 96

Slide 96 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return [$this, 'emailValidation']; } return [$this, 'genericValidation']; } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 97

Slide 97 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return Closure::fromCallable([$this, 'emailValidation']); } return Closure::fromCallable([$this, 'genericValidation']); } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 98

Slide 98 text

C L O S I N G O V E R S C O P E class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return Closure::fromCallable([$this, 'emailValidation']); } return Closure::fromCallable([$this, 'genericValidation']); } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 99

Slide 99 text

B E T T E R E R R O R H A N D L I N G

Slide 100

Slide 100 text

B E T T E R E R R O R H A N D L I N G class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return Closure::fromCallable([$this, 'emailValidation']); } return Closure::fromCallable([$this, 'genericValidation']); } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 101

Slide 101 text

B E T T E R E R R O R H A N D L I N G class Validator { public function getValidatorCallback($validationType) { if ($validationType == 'email') { return Closure::fromCallable([$this, 'emailValdation']); } return Closure::fromCallable([$this, 'genericValidation']); } private function emailValidation($userData) {...} private function genericValidation($userData) {...} } $validator = new Validator(); $callback = $validator->getValidatorCallback('email'); $callback($userData);

Slide 102

Slide 102 text

B E T T E R E R R O R H A N D L I N G TypeError: Failed to create closure from callable: class 'Validator' does not have a method 'emailValdation' in :

Slide 103

Slide 103 text

B E T T E R E R R O R H A N D L I N G TypeError: Failed to create closure from callable: function 'foo' not found or invalid function name in :

Slide 104

Slide 104 text

C L A S S C O N S T A N T V I S I B I L I T Y

Slide 105

Slide 105 text

C L A S S C O N S T A N T V I S I B I L I T Y class MyClass { const MY_CONSTANT = 0; const MY_OTHER_CONSTANT = 1; const MY_FAVORITE_CONSTANT = 2; const FOO = 1, BAR = 2; }

Slide 106

Slide 106 text

C L A S S C O N S T A N T V I S I B I L I T Y class MyClass { public const MY_CONSTANT = 0; protected const MY_OTHER_CONSTANT = 1; private const MY_FAVORITE_CONSTANT = 2; private const FOO = 1, BAR = 2; }

Slide 107

Slide 107 text

C L A S S C O N S T A N T V I S I B I L I T Y class MyClass { public const MY_CONSTANT = 0; protected const MY_OTHER_CONSTANT = 1; private const MY_FAVORITE_CONSTANT = 2; private const FOO = 1, BAR = 2; }

Slide 108

Slide 108 text

C L A S S C O N S T A N T V I S I B I L I T Y class MyClass { public const MY_CONSTANT = 0; protected const MY_OTHER_CONSTANT = 1; private const MY_FAVORITE_CONSTANT = 2; private const FOO = 1, BAR = 2; }

Slide 109

Slide 109 text

C L A S S C O N S T A N T V I S I B I L I T Y class MyClass { public const MY_CONSTANT = 0; protected const MY_OTHER_CONSTANT = 1; private const MY_FAVORITE_CONSTANT = 2; private const FOO = 1, BAR = 2; }

Slide 110

Slide 110 text

I N T E R F A C E C O N S T A N T V I S I B I L I T Y interface MyInterface { public const MY_CONSTANT = 0; protected const MY_OTHER_CONSTANT = 1; private const MY_FAVORITE_CONSTANT = 2; private const FOO = 1, BAR = 2; }

Slide 111

Slide 111 text

E N H A N C E M E N T S T O R E F L E C T I O N • Added ReflectionClass->getReflectionConstant() • Added ReflectionClass->getReflectionConstants() • Both return ReflectionClassConstant instances

Slide 112

Slide 112 text

E N H A N C E M E N T S T O R E F L E C T I O N class ReflectionClassConstant { public function getName() { } public function getValue() { } public function isPublic() { } public function isPrivate() { } public function isProtected() { } public function getModifiers() { } public function getDeclaringClass() { } public function getDocComment() { } }

Slide 113

Slide 113 text

C A T C H I N G M U L T I P L E E X C E P T I O N S

Slide 114

Slide 114 text

C A T C H I N G M U L T I P L E E X C E P T I O N S try { ... } catch (\PDOException $e) { \My\App::renderError(500); } catch (\My\RequestException $e) { \My\App::renderError(500); } catch (\My\RequestMethodException $e) { \My\App::renderError(405); }

Slide 115

Slide 115 text

C A T C H I N G M U L T I P L E E X C E P T I O N S try { ... } catch (\PDOException $e) { \My\App::renderError(500); } catch (\My\RequestException $e) { \My\App::renderError(500); } catch (\My\RequestMethodException $e) { \My\App::renderError(405); }

Slide 116

Slide 116 text

C A T C H I N G M U L T I P L E E X C E P T I O N S try { ... } catch (\PDOException $e) { \My\App::renderError(500); } catch (\My\RequestException $e) { \My\App::renderError(500); } catch (\My\RequestMethodException $e) { \My\App::renderError(405); }

Slide 117

Slide 117 text

C A T C H I N G M U L T I P L E E X C E P T I O N S try { ... } catch (\PDOException $e) { \My\App::renderError(500); } catch (\My\RequestException $e) { \My\App::renderError(500); } catch (\My\RequestMethodException $e) { \My\App::renderError(405); }

Slide 118

Slide 118 text

C A T C H I N G M U L T I P L E E X C E P T I O N S try { ... } catch (\PDOException $e) { \My\App::renderError(500); } catch (\My\RequestException $e) { \My\App::renderError(500); } catch (\My\RequestMethodException $e) { \My\App::renderError(405); }

Slide 119

Slide 119 text

C A T C H I N G M U L T I P L E E X C E P T I O N S try { ... } catch (\PDOException | \My\RequestException $e) { \My\App::renderError(500); } catch (\My\RequestMethodException $e) { \My\App::renderError(405); }

Slide 120

Slide 120 text

L I S T ( ) I M P R O V E M E N T S

Slide 121

Slide 121 text

S P E C I F Y I N G K E Y S I N L I S T ( )

Slide 122

Slide 122 text

S Y D N E Y

Slide 123

Slide 123 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.8688° S', 'long' => '151.2093° E' ]; list($lat, $long) = $coords; var_dump($lat, $long);

Slide 124

Slide 124 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.8688° S', 'long' => '151.2093° E' ]; list($lat, $long) = $coords; var_dump($lat, $long); Notice: Undefined offset: 0 Notice: Undefined offset: 1

Slide 125

Slide 125 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.8688° S', 'long' => '151.2093° E' ]; list($lat, $long) = $coords; var_dump($lat, $long); $lat === NULL $long === NULL

Slide 126

Slide 126 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.8688° S', 'long' => '151.2093° E' ]; list($lat, $long) = $coords; var_dump($lat, $long);

Slide 127

Slide 127 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.8688° S', 'long' => '151.2093° E' ]; list('lat' => $lat, 'long' => $long) = $coords; var_dump($lat, $long);

Slide 128

Slide 128 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.8688° S', 'long' => '151.2093° E' ]; list('lat' => $lat, 'long' => $long) = $coords; var_dump($lat, $long); $lat === '33.8688° S' $long === '151.2093° E'

Slide 129

Slide 129 text

S I N G A P O R E

Slide 130

Slide 130 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '1.3521° N', 'long' => '103.8198° E' ]; list($lat, $long) = $coords; var_dump($lat, $long);

Slide 131

Slide 131 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '1.3521° N', 'long' => '103.8198° E' ]; list($lat, $long) = $coords; var_dump($lat, $long); Notice: Undefined offset: 0 Notice: Undefined offset: 1

Slide 132

Slide 132 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '1.3521° N', 'long' => '103.8198° E' ]; list($lat, $long) = $coords; var_dump($lat, $long); $lat === NULL $long === NULL

Slide 133

Slide 133 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '1.3521° N', 'long' => '103.8198° E' ]; list($lat, $long) = $coords; var_dump($lat, $long);

Slide 134

Slide 134 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '1.3521° N', 'long' => '103.8198° E' ]; list('lat' => $lat, 'long' => $long) = $coords; var_dump($lat, $long);

Slide 135

Slide 135 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '1.3521° N', 'long' => '103.8198° E' ]; list('lat' => $lat, 'long' => $long) = $coords; var_dump($lat, $long); $lat === '1.3521° N' $long === '103.8198° E'

Slide 136

Slide 136 text

A T L A N T A

Slide 137

Slide 137 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.7490° N', 'long' => '84.3880° W' ]; list($lat, $long) = $coords; var_dump($lat, $long);

Slide 138

Slide 138 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.7490° N', 'long' => '84.3880° W' ]; list($lat, $long) = $coords; var_dump($lat, $long); Notice: Undefined offset: 0 Notice: Undefined offset: 1

Slide 139

Slide 139 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.7490° N', 'long' => '84.3880° W' ]; list($lat, $long) = $coords; var_dump($lat, $long); $lat === NULL $long === NULL

Slide 140

Slide 140 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.7490° N', 'long' => '84.3880° W' ]; list($lat, $long) = $coords; var_dump($lat, $long);

Slide 141

Slide 141 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.7490° N', 'long' => '84.3880° W' ]; list('lat' => $lat, 'long' => $long) = $coords; var_dump($lat, $long);

Slide 142

Slide 142 text

S P E C I F Y I N G K E Y S I N L I S T ( ) $coords = [ 'lat' => '33.7490° N', 'long' => '84.3880° W' ]; list('lat' => $lat, 'long' => $long) = $coords; var_dump($lat, $long); $lat === '33.7490° N’ $long === '84.3880° W'

Slide 143

Slide 143 text

S K I P P I N G N U M E R I C K E Y S

Slide 144

Slide 144 text

S K I P P I N G N U M E R I C K E Y S list( , $lat, $long, , , $name ) = getLocation();

Slide 145

Slide 145 text

S K I P P I N G N U M E R I C K E Y S list( , $lat, $long, , , $name ) = getLocation();

Slide 146

Slide 146 text

S K I P P I N G N U M E R I C K E Y S list( 1 => $lat, 2 => $long, 5 => $name ) = getLocation();

Slide 147

Slide 147 text

S H O R T L I S T ( ) S Y N T A X

Slide 148

Slide 148 text

S H O R T L I S T ( ) S Y N T A X list('lat' => $lat, 'long' => $long) = $coords;

Slide 149

Slide 149 text

S H O R T L I S T ( ) S Y N T A X ['lat' => $lat, 'long' => $long] = $coords;

Slide 150

Slide 150 text

S H O R T L I S T ( ) S Y N T A X : M U T U A L L Y E X C L U S I V E S Y N T A X // This is not allowed: list([$a, $b], [$c, $d]) = [[1, 2], [3, 4]]; // This is also not allowed: [list($a, $b), list($c, $d)] = [[1, 2], [3, 4]]; // This, however, is allowed: [[$a, $b], [$c, $d]] = [[1, 2], [3, 4]]; // This also: list(list($a, $b), list($c, $d)) = [[1, 2], [3, 4]];

Slide 151

Slide 151 text

I T E R A B L E P S U E D O T Y P E

Slide 152

Slide 152 text

I T E R A B L E P S U E D O T Y P E • Similar to callable but values you can foreach, or yield • Accepts any array, or object that implements Traversable • SPL Iterators • Generators • Can be used for return value • Not generic objects

Slide 153

Slide 153 text

I T E R A B L E P S U E D O T Y P E function enumerate(iterable $args) { foreach ($args as $arg) { echo $args; } }

Slide 154

Slide 154 text

I T E R A B L E P S U E D O T Y P E function enumerate(iterable $args) { foreach ($args as $arg) { echo $args; } }

Slide 155

Slide 155 text

I T E R A B L E P S U E D O T Y P E : R E T U R N T Y P E function compactor(... $args): iterable { return $args; }

Slide 156

Slide 156 text

I T E R A B L E P S U E D O T Y P E : R E T U R N T Y P E function compactor(... $args): iterable { return $args; }

Slide 157

Slide 157 text

I T E R A B L E P S U E D O T Y P E : G E N E R A T O R S function compactor(... $args): iterable { foreach ($args as $arg) { yield $arg; } }

Slide 158

Slide 158 text

A D D S I S _ I T E R A B L E ( ) F U N C T I O N // true is_iterable([1, 2, 3]); is_iterable(new ArrayIterator([1, 2, 3])); is_iterable((function () { yield 1; })()); // false is_iterable(1); is_iterable("Hello World"); is_iterable(new stdClass());

Slide 159

Slide 159 text

V O I D R E T U R N T Y P E

Slide 160

Slide 160 text

V O I D R E T U R N T Y P E • PHP functions implicitly return null • There is a semantic difference between returning null, and returning nothing (or not returning) • Void enforces this behavior • Can only be used as a return type • Cannot be changed in sub-classes

Slide 161

Slide 161 text

function return_nothing(): void { return null; } V O I D R E T U R N T Y P E

Slide 162

Slide 162 text

function return_nothing(): void { return null; } V O I D R E T U R N T Y P E

Slide 163

Slide 163 text

function return_nothing(): void { return null; } V O I D R E T U R N T Y P E

Slide 164

Slide 164 text

V O I D R E T U R N T Y P E function return_nothing(): void { return null; } Fatal error: A void function must not return a value (did you mean "return;" instead of "return null;"?)

Slide 165

Slide 165 text

V O I D R E T U R N T Y P E function return_nothing(): void { return "nothing"; } Fatal error: A void function must not return a value

Slide 166

Slide 166 text

V O I D R E T U R N T Y P E function return_nothing(): void { return; } // or function return_nothing(): void { // no return }

Slide 167

Slide 167 text

N U L L A B L E T Y P E S

Slide 168

Slide 168 text

N U L L A B L E T Y P E S • Allow specified type, or null • Requires an explicit null, doesn’t default to null • Works for Arguments & Return Types • Prefix type with a ? (question mark)

Slide 169

Slide 169 text

function hello(string $whom) { echo "Hello " . ($whom ?? "World"); } hello(); N U L L A B L E T Y P E S

Slide 170

Slide 170 text

function hello(string $whom) { echo "Hello " . ($whom ?? "World"); } hello(); N U L L A B L E T Y P E S

Slide 171

Slide 171 text

function hello(string $whom) { echo "Hello " . ($whom ?? "World"); } hello(); N U L L A B L E T Y P E S

Slide 172

Slide 172 text

N U L L A B L E T Y P E S function hello(string $whom) { echo "Hello " . ($whom ?? "World"); } hello(); Fatal error: Uncaught ArgumentCountError: Too few arguments to function hello(), 0 passed in on line 6 and exactly 1 expected in :1

Slide 173

Slide 173 text

N U L L A B L E T Y P E S function hello(string $whom = null) { echo "Hello " . ($whom ?? "World"); } hello();

Slide 174

Slide 174 text

N U L L A B L E T Y P E S function hello(string $whom = null) { echo "Hello " . ($whom ?? "World"); } hello();

Slide 175

Slide 175 text

function hello(?string $whom) { echo "Hello " . ($whom ?? "World"); } hello(); N U L L A B L E T Y P E S

Slide 176

Slide 176 text

function hello(?string $whom) { echo "Hello " . ($whom ?? "World"); } hello(); N U L L A B L E T Y P E S

Slide 177

Slide 177 text

N U L L A B L E T Y P E S function hello(?string $whom) { echo "Hello " . ($whom ?? "World"); } hello(); Fatal error: Uncaught ArgumentCountError: Too few arguments to function hello(), 0 passed in on line 6 and exactly 1 expected in :1

Slide 178

Slide 178 text

N U L L A B L E T Y P E S function hello(?string $whom) { echo "Hello " . ($whom ?? "World"); } hello(null);

Slide 179

Slide 179 text

N U L L A B L E T Y P E S function hello(?string $whom = null) { echo "Hello " . ($whom ?? "World"); } hello(null);

Slide 180

Slide 180 text

N U L L A B L E T Y P E S function hello(?string $whom = null) { echo "Hello " . ($whom ?? "World"); } hello();

Slide 181

Slide 181 text

N U L L A B L E R E T U R N T Y P E S

Slide 182

Slide 182 text

N U L L A B L E T Y P E S function hello(?string $whom): string { if ($whom !== null) { return "Hello $whom"; } } echo hello("World"); // Hello World

Slide 183

Slide 183 text

function hello(?string $whom): string { if ($whom !== null) { return "Hello $whom"; } } echo hello("World"); // Hello World N U L L A B L E T Y P E S

Slide 184

Slide 184 text

function hello(?string $whom): string { if ($whom !== null) { return "Hello $whom"; } } echo hello("World"); // Hello World N U L L A B L E T Y P E S

Slide 185

Slide 185 text

N U L L A B L E T Y P E S function hello(?string $whom): string { if ($whom !== null) { return "Hello $whom"; } } echo hello(null);

Slide 186

Slide 186 text

N U L L A B L E T Y P E S TypeError: Return value of hello() must be of the type string, none returned

Slide 187

Slide 187 text

N U L L A B L E T Y P E S function hello(?string $whom): string { if ($whom !== null) { return "Hello $whom"; } } echo hello(null);

Slide 188

Slide 188 text

N U L L A B L E T Y P E S function hello(?string $whom): ?string { if ($whom !== null) { return "Hello $whom"; } } echo hello(null);

Slide 189

Slide 189 text

N U L L A B L E T Y P E S TypeError: Return value of hello() must be of the type string, none returned

Slide 190

Slide 190 text

N U L L A B L E T Y P E S function hello(?string $whom): ?string { if ($whom !== null) { return "Hello $whom"; } } echo hello(null);

Slide 191

Slide 191 text

N U L L A B L E T Y P E S function hello(?string $whom): ?string { if ($whom !== null) { return "Hello $whom"; } return; } echo hello(null);

Slide 192

Slide 192 text

N U L L A B L E T Y P E S Fatal error: A function with return type must return a value (did you mean "return null;" instead of "return;"?)

Slide 193

Slide 193 text

N U L L A B L E T Y P E S function hello(?string $whom): ?string { if ($whom !== null) { return "Hello $whom"; } return; } echo hello(null);

Slide 194

Slide 194 text

N U L L A B L E T Y P E S function hello(?string $whom): ?string { if ($whom !== null) { return "Hello $whom"; } return null; } echo hello(null); // null

Slide 195

Slide 195 text

C U R R E N T : P H P 7 . 1 . 5

Slide 196

Slide 196 text

B E Y O N D P H P 7 . 1

Slide 197

Slide 197 text

No content

Slide 198

Slide 198 text

C U R R E N T L Y 1 5 R F C S I M P L E M E N T E D

Slide 199

Slide 199 text

8 M O R E A C C E P T E D ( A N D O N E F O R P H P 8 . 0 ! )

Slide 200

Slide 200 text

1 4 M O R E I N D I S C U S S I O N ( A N D A N O T H E R O N E F O R P H P 8 . 0 ! )

Slide 201

Slide 201 text

W E W A N T Y O U ! Credit: NASA

Slide 202

Slide 202 text

I N S U M M A R Y

Slide 203

Slide 203 text

I S G R E A T !

Slide 204

Slide 204 text

C O M I N G S O O N !

Slide 205

Slide 205 text

T H E F U T U R E I S A W E S O M E ! CC-BY-SA: Steven Gerner

Slide 206

Slide 206 text

F E E D B A C K Feedback: Twitter: Email: Slides: https://joind.in/talk/ @dshafik [email protected] http://daveyshafik.com/slides f33ff