Slide 1

Slide 1 text

Typed PHP

Slide 2

Slide 2 text

1. PHP types

Slide 3

Slide 3 text

Weak types ⌁ declare/assign variables without a type ⌁ change variable type any time

Slide 4

Slide 4 text

Weak types $user = 1; // ...and then $user = "Chris";

Slide 5

Slide 5 text

Zval ⌁ stores variable and type data ⌁ ...also garbage collection information

Slide 6

Slide 6 text

Zval struct _zval_struct { zvalue_value value; zend_uint refcount__gc; zend_uchar type; zend_uchar is_ref__gc; };

Slide 7

Slide 7 text

Equality ⌁ double-equals coerces types ⌁ triple-equals is probably what you want

Slide 8

Slide 8 text

Equality true == 1; // true true === 1; // false

Slide 9

Slide 9 text

Type hinting ⌁ helps prevent type errors ⌁ unsupported for scalar types

Slide 10

Slide 10 text

Type hinting function map(array $items, callable $apply) { // ...call $apply on each item }

Slide 11

Slide 11 text

Type hinting function shorten($string, $length) { assert(is_string($string)); assert(is_integer($length)); // ...reduce $string to $length }

Slide 12

Slide 12 text

2. Inconsistencies

Slide 13

Slide 13 text

Underscores and abbreviations ⌁ 98 string functions ⌁ 30 with underscores, 68 without ⌁ 55 with abbreviations, 43 without

Slide 14

Slide 14 text

Argument order ⌁ array is needle/haystack ⌁ string is haystack/needle ⌁ ...except when it's not

Slide 15

Slide 15 text

Argument order array_filter(array $items, callable $apply); array_map(callable $apply, array $items); array_reduce(array $items, callable $apply);

Slide 16

Slide 16 text

Argument order strstr($haystack, $needle); explode($needle, $haystack); str_replace($needle, $replace, $haystack);

Slide 17

Slide 17 text

Return values ⌁ not always what you think ⌁ manual isn't always clear

Slide 18

Slide 18 text

Return values preg_match returns 1 if a pattern matches a given subject, 0 if a pattern does not match a given subject. Or false if there was an error.

Slide 19

Slide 19 text

Return values preg_match returns 1 if a pattern matches a given subject, 0 if a pattern does not match a given subject. Or false if there was an error.

Slide 20

Slide 20 text

Return values Oh, and by the way: "This function may return Boolean FALSE, but may also return a non- Boolean value which evaluates to FALSE..."

Slide 21

Slide 21 text

Return values Oh, and by the way: "This function may return Boolean FALSE, but may also return a non- Boolean value which evaluates to FALSE..."

Slide 22

Slide 22 text

3 .Boxing

Slide 23

Slide 23 text

Boxing ⌁ classes wrap primatives ⌁ special construction ⌁ special deconstruction

Slide 24

Slide 24 text

Boxing class StringObject { protected $data; public function __construct($data) { $this->data = $data; }

Slide 25

Slide 25 text

Boxing public function value() { return $this->data; } }

Slide 26

Slide 26 text

Boxing $string = new StringObject("hello world"); $string->value(); // "hello world" function value(StringObject $string) { return $string->value(); }

Slide 27

Slide 27 text

Boxing ⌁ requires special construction and deconstruction ⌁ allows type-hinting

Slide 28

Slide 28 text

4. Scalar types

Slide 29

Slide 29 text

Scalar types ⌁ classes wrap primatives (again) ⌁ automatic constrution ⌁ automatic deconstruction

Slide 30

Slide 30 text

Scalar types class StringObject { public function length() { return strlen($this); } }

Slide 31

Slide 31 text

Scalar types register_primitive_type_handler( "string", "StringObject" ); $string = "hello world"; $string->length(); // 11

Slide 32

Slide 32 text

Scalar types ⌁ automatic construction and deconstruction ⌁ doesn't allow type-hinting ⌁ can't opt-out

Slide 33

Slide 33 text

5. SPL types

Slide 34

Slide 34 text

SPL types ⌁ classes wrap primatives (again) ⌁ special constrution ⌁ automatic deconstruction

Slide 35

Slide 35 text

SPL types class StringObject extends SplString { public function length() { return strlen($this); } }

Slide 36

Slide 36 text

SPL types $string = new StringObject("hello world"); "big " . $string; // "big hello world"

Slide 37

Slide 37 text

SPL types ⌁ special construction ⌁ automatic deconstruction ⌁ allows type-hinting ⌁ must opt-in

Slide 38

Slide 38 text

SPL types This is my favourite!

Slide 39

Slide 39 text

6. Optional types

Slide 40

Slide 40 text

Optional types ⌁ classes wrap classes ⌁ reduce type checking ⌁ reduce errors

Slide 41

Slide 41 text

Optional types $user = User::find(1); if ($user !== null) $address = $user->address(); if ($address !== null) $city = $address->city(); if ($city !== null) $forecast = $city->forecast(); print "Your forecast: " . $forecast;

Slide 42

Slide 42 text

Optional types $user = User::find(1); if ($user !== null) $address = $user->address(); if ($address !== null) $city = $address->city(); if ($city !== null) $forecast = $city->forecast(); print "Your forecast: " . $forecast;

Slide 43

Slide 43 text

Optional types $user = new Optional(User::find(1)); $user ->address()->city()->forecast() ->value(function($forecast) { print "Your forecast: " . $forecast; });

Slide 44

Slide 44 text

Optional types It gets alot better than that...but no time!

Slide 45

Slide 45 text

7. Learning more

Slide 46

Slide 46 text

Learning more ⌁ Anthony Ferrara @ircmaxell ⌁ Igor Wiedler @igorwhiletrue ⌁ Christopher Pitt @assertchris ⌁ joind.in/talk/view/13094