Slide 1

Slide 1 text

Strict programming in PHP by Gabriel Caruso phpCE 2018

Slide 2

Slide 2 text

Backend Developer at Leroy Merlin PHP Internals and Externals Evangelist PHPSP Member @carusogabriel

Slide 3

Slide 3 text

Agenda - What does strict programming means - Benefits of using a strict programming language - Coding strict with PHP - How to ensure that my project is strict safe - Strict changes for PHP’s core

Slide 4

Slide 4 text

What does strict programming mean?

Slide 5

Slide 5 text

Differences Strict programming A way, a paradigm, in how we can treat our coding Strict programming language A language that is build on top of strict programming paradigm

Slide 6

Slide 6 text

Benefits of using a strict programming language

Slide 7

Slide 7 text

- Built-in scalar types - More strongly-typed code - Closed API code - Less Runtime exceptions - Know the flow of your code

Slide 8

Slide 8 text

strict programming in PHP

Slide 9

Slide 9 text

Equal operator

Slide 10

Slide 10 text

Equal operator

Slide 11

Slide 11 text

Equal operator

Slide 12

Slide 12 text

Equal operator

Slide 13

Slide 13 text

Equal operator

Slide 14

Slide 14 text

Equal operator

Slide 15

Slide 15 text

Equal operator

Slide 16

Slide 16 text

Equal operator

Slide 17

Slide 17 text

Equal operator

Slide 18

Slide 18 text

Identical operator

Slide 19

Slide 19 text

Slide 20

Slide 20 text

Slide 21

Slide 21 text

Slide 22

Slide 22 text

Slide 23

Slide 23 text

Slide 24

Slide 24 text

Slide 25

Slide 25 text

Empty() Function

Slide 26

Slide 26 text

Use PHP instead!

Slide 27

Slide 27 text

Slide 28

Slide 28 text

Slide 29

Slide 29 text

is_null() only checks for the null type

Slide 30

Slide 30 text

is_null() only checks for the null type

Slide 31

Slide 31 text

But use the identical operator when you aren’t sure

Slide 32

Slide 32 text

Slide 33

Slide 33 text

switch conditions are loosely compare

Slide 34

Slide 34 text

Slide 35

Slide 35 text

if conditions aren’t that trustable

Slide 36

Slide 36 text

Only pass booleans to if conditions

Slide 37

Slide 37 text

What about the php 7 scalar types?

Slide 38

Slide 38 text

Weak scalar types do casting

Slide 39

Slide 39 text

Use Strict scalar types!

Slide 40

Slide 40 text

*Only non-NaN floats between PHP_INT_MIN and PHP_INT_MAX are accepted †Non-numeric strings are not accepted ‡Only if the object has a __toString() method inductions of scalar types

Slide 41

Slide 41 text

How to ensure that my project is strict safe?

Slide 42

Slide 42 text

Static analysis

Slide 43

Slide 43 text

Coding standards

Slide 44

Slide 44 text

strict changes for PHP’s core?

Slide 45

Slide 45 text

Typed Properties in PHP 7.4

Slide 46

Slide 46 text

References you can study beyond this talk - https://wiki.php.net/rfc/scalar_type_hints_v5#behaviour_o f_weak_type_checks - https://php.net/types-comparisons - https://www.phparch.com/2018/04/testing-strategy-with-the -help-of-static-analysis/ - https://github.com/phpstan/phpstan-strict-rules - https://github.com/slevomat/coding-standard

Slide 47

Slide 47 text

Thank you ♡ Questions? @carusogabriel