Slide 1

Slide 1 text

Modern PHP

Slide 2

Slide 2 text

Anwar ZIANI https://twitter.com/Zianwar_ Web development ❤️ Computer Engineering Student at ENSAT Who am I ?

Slide 3

Slide 3 text

PHP is a server side scripting language , your write php code, upload it to a server and execute it with an interpreter like Apache or nginx . PHP

Slide 4

Slide 4 text

PHP is a server side scripting language , your write php code, upload it to a server and execute it with an interpreter like Apache or nginx . PHP can also be used to build command line apps (just like bash, Ruby or Python) PHP

Slide 5

Slide 5 text

PHP was created by Rasmus Lerdorf, as a collection of CGI scripts to track visits to his online resume. How it started

Slide 6

Slide 6 text

PHP was created by Rasmus Lerdorf, as a collection of CGI scripts to track visits to his online resume. How it started He named this set of CGI scripts by “Personal Home Page Tools”

Slide 7

Slide 7 text

PHP was created by Rasmus Lerdorf, as a collection of CGI scripts to track visits to his online resume. How it started He named this set of CGI scripts by “Personal Home Page Tools” After that he developed this collection to become a Full programming language with more consistent syntax and OOP support

Slide 8

Slide 8 text

PHP was created by Rasmus Lerdorf, as a collection of CGI scripts to track visits to his online resume. How it started He named this set of CGI scripts by “Personal Home Page Tools” After that he developed this collection to become a Full programming language with more consistent syntax and OOP support After that they launched PHP 3 under the name of Hypertext Preprocessor

Slide 9

Slide 9 text

PHP is so popular, supported by many developers and companies. (Wikipedia, Facebook, Yahoo!…) The present of PHP

Slide 10

Slide 10 text

PHP is so popular, supported by many developers and companies. (Wikipedia, Facebook, Yahoo!…) The present of PHP Nowadays: no FTP, version control software like Git helps maintain and track entire codebase

Slide 11

Slide 11 text

PHP is so popular, supported by many developers and companies. (Wikipedia, Facebook, Yahoo!…) The present of PHP Nowadays: no FTP, version control software like Git helps maintain and track entire codebase Local environment identical to production with tools like Vagrant, Chef, Puppet…

Slide 12

Slide 12 text

PHP is so popular, supported by many developers and companies. (Wikipedia, Facebook, Yahoo!…) Composer as a dependency manager The present of PHP Nowadays: no FTP, version control software like Git helps maintain and track entire codebase Local environment identical to production with tools like Vagrant, Chef, Puppet…

Slide 13

Slide 13 text

vs Zend Engine Hip Hip Virtual machine The futur of PHP

Slide 14

Slide 14 text

Namespaces definition Important tool to organise PHP code into a virtual hierarchy +PVTQFWEGFKP 2*2

Slide 15

Slide 15 text

Namespaces definition Important tool to organise PHP code into a virtual hierarchy +PVTQFWEGFKP 2*2 Just like operating system’s filesystem directory structure

Slide 16

Slide 16 text

Namespaces definition Important tool to organise PHP code into a virtual hierarchy +PVTQFWEGFKP 2*2 Just like operating system’s filesystem directory structure Avoid any conflict with any class names used by another developers

Slide 17

Slide 17 text

Namespaces symfony/httpfoundation Symfony framework’s component that manages HTTP requests and responses : %QORQPGPV definition

Slide 18

Slide 18 text

Namespaces symfony/httpfoundation Symfony framework’s component that manages HTTP requests and responses : class Request class Response %QORQPGPV definition

Slide 19

Slide 19 text

Symfony\Component\Httpfoundation Namespaces why ?

Slide 20

Slide 20 text

Symfony\Component\Httpfoundation ENSAT\Routing Namespaces why ?

Slide 21

Slide 21 text

Symfony\Component\Httpfoundation ENSAT\Routing class Request class Response ... { Namespaces why ?

Slide 22

Slide 22 text

Symfony\Component\Httpfoundation ENSAT\Routing class Request class Response class AnotherClass ... { class Request class Response ... { Namespaces why ?

Slide 23

Slide 23 text

Symfony\Component\Httpfoundation ENSAT\Routing class Request class Response class AnotherClass ... { class Request class Response ... { Namespaces why ? '4414

Slide 24

Slide 24 text

Slide 25

Slide 25 text

Slide 26

Slide 26 text

Slide 27

Slide 27 text

Slide 28

Slide 28 text

Slide 29

Slide 29 text

Namespaces declaration

Slide 30

Slide 30 text

Namespaces declaration

Slide 31

Slide 31 text

Namespaces declaration

Slide 32

Slide 32 text

Namespaces declaration

Slide 33

Slide 33 text

Namespaces use

Slide 34

Slide 34 text

Namespaces use

Slide 35

Slide 35 text

Namespaces use

Slide 36

Slide 36 text

Slide 37

Slide 37 text

Slide 38

Slide 38 text

Slide 39

Slide 39 text

Namespaces alias

Slide 40

Slide 40 text

Namespaces alias

Slide 41

Slide 41 text

Namespaces alias

Slide 42

Slide 42 text

Namespaces default namespace

Slide 43

Slide 43 text

Namespaces default namespace

Slide 44

Slide 44 text

Namespaces default namespace

Slide 45

Slide 45 text

Namespaces default namespace

Slide 46

Slide 46 text

Traits definition +PVTQFWEGFKP 2*2 • Partial class implementation • Look like an interface, behave like a class • Cannot be instantiated on it’s own

Slide 47

Slide 47 text

Traits why ? class A class C class B

Slide 48

Slide 48 text

Traits why ? class A class C class B

Slide 49

Slide 49 text

Traits why ? class AbstractFoo

Slide 50

Slide 50 text

Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class Foo1 extends AbstractFoo

Slide 51

Slide 51 text

Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class Foo1 extends AbstractFoo class AbstractBar

Slide 52

Slide 52 text

Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class Foo1 extends AbstractFoo class AbstractBar class Bar2 extends AbstractBar class Bar1 extends AbstractBar

Slide 53

Slide 53 text

Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class Foo1 extends AbstractFoo class AbstractBar class Bar2 extends AbstractBar class Bar1 extends AbstractBar Shareable,

Slide 54

Slide 54 text

Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class Foo1 extends AbstractFoo class AbstractBar class Bar2 extends AbstractBar class Bar1 extends AbstractBar Shareable, Likeable,

Slide 55

Slide 55 text

Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class Foo1 extends AbstractFoo class AbstractBar class Bar2 extends AbstractBar class Bar1 extends AbstractBar Shareable, Likeable, Geocodable …

Slide 56

Slide 56 text

Traits declaration

Slide 57

Slide 57 text

Traits declaration

Slide 58

Slide 58 text

Traits declaration

Slide 59

Slide 59 text

Traits declaration share();

Slide 60

Slide 60 text

Traits examples Illuminate\Auth\Authenticatable Examples of Traits in Laravel framework :

Slide 61

Slide 61 text

Traits examples Illuminate\Auth\Authenticatable Illuminate\Auth\Reminders\RemindableTrait Examples of Traits in Laravel framework :

Slide 62

Slide 62 text

Traits examples Illuminate\Auth\Authenticatable Illuminate\Auth\Reminders\RemindableTrait Illuminate\Auth\UserTrait Examples of Traits in Laravel framework :

Slide 63

Slide 63 text

Traits examples Illuminate\Auth\Authenticatable Illuminate\Auth\Reminders\RemindableTrait Illuminate\Auth\UserTrait Laravel\Cashier\Billable Examples of Traits in Laravel framework :

Slide 64

Slide 64 text

Closures definition +PVTQFWEGFKP 2*2 • A closure is a function that encapsulates its surrounding state at the time it is created. • The encapsulated state exists inside the closure even when the closure lives after its original environment ceases to exist.

Slide 65

Slide 65 text

Closures definition +PVTQFWEGFKP 2*2 • A closure is a function that encapsulates its surrounding state at the time it is created. • The encapsulated state exists inside the closure even when the closure lives after its original environment ceases to exist. • Anonymous functions • Can be assigned to variables and passed around just like any other PHP object • They are actually objects : instances of the Closure class

Slide 66

Slide 66 text

Closures examples

Slide 67

Slide 67 text

Closures examples

Slide 68

Slide 68 text

Closures examples

Slide 69

Slide 69 text

Closures examples "Mustapha, get me some coffee!” Attaching state to closures :

Slide 70

Slide 70 text

Closures examples Examples of Closures in Laravel framework :

Slide 71

Slide 71 text

Closures examples Examples of Closures in Laravel framework :

Slide 72

Slide 72 text

Closures examples Examples of Closures in Laravel framework :

Slide 73

Slide 73 text

Built-in HTTP server defintion +PVTQFWEGFKP 2*2 • You don’t need to Apache or ngnix to preview your PHP code • Useful for small previews, but not full development

Slide 74

Slide 74 text

Built-in HTTP server usage zianwar$ php -S localhost:4000 In terminal, navigate to your project root and type :

Slide 75

Slide 75 text

Built-in HTTP server usage zianwar$ php -S localhost:4000 In terminal, navigate to your project root and type :

Slide 76

Slide 76 text

Standards intro Laravel Symfony Silex Slim CodeIgniter } } /CETQHTCOGYQTMU /KETQHTCOGYQTMU .GICE[HTCOGYQTM

Slide 77

Slide 77 text

Standards intro Apparition of PHP Framework Interop Group (PHP-FIG)

Slide 78

Slide 78 text

Standards intro Apparition of PHP Framework Interop Group (PHP-FIG) According to the PHP-FIG website, they “talk about the commonalities between our projects and find ways we can work together.”

Slide 79

Slide 79 text

Standards intro • Interfaces • Autoloading • Style Modern framework share different standards :

Slide 80

Slide 80 text

Standards PSR • PSR-1 : Basic code style • PSR-2 : Strict code style • PSR-3 : Logger interface • PSR-4 : Autoloading PHP standards recommendations

Slide 81

Slide 81 text

Standards PSR PSR-1 : Basic Code Style : 'CU[VQ KORNGOGPV http://www.php-fig.org/psr/psr-1/

Slide 82

Slide 82 text

Standards PSR PSR-2 : Strict Code Style : http://www.php-fig.org/psr/psr-2/

Slide 83

Slide 83 text

Standards PSR

Slide 84

Slide 84 text

Standards PSR

Slide 85

Slide 85 text

Standards PSR PSR-4 : Autoloader : Autoloading classes from file paths

Slide 86

Slide 86 text

Standards PSR PSR-4 Autoloader Strategy : Mapping the top-level namespace prefix to a specific filesystem directory

Slide 87

Slide 87 text

Standards PSR PSR-4 Autoloader Strategy : Mapping the top-level namespace prefix to a specific filesystem directory Acme\ src/ PCOGURCEG FKTGEVQT[

Slide 88

Slide 88 text

Standards PSR PSR-4 Autoloader Strategy : Mapping the top-level namespace prefix to a specific filesystem directory Acme\ src/ PCOGURCEG FKTGEVQT[ Acme\Something src/Something UWDFKTGEVQT[ UWDPCOGURCEG

Slide 89

Slide 89 text

Standards PSR

Slide 90

Slide 90 text

Standards PSR

Slide 91

Slide 91 text

Standards PSR … "autoload": { "classmap": [ "database" ], }, … composer.json Laravel is using PSR-4 : "psr-4": { "App\\": "app/" }

Slide 92

Slide 92 text

Standards PSR … "autoload": { "classmap": [ "database" ], }, … composer.json Laravel is using PSR-4 : "psr-4": { "App\\": "app/" }

Slide 93

Slide 93 text

Component oriented development definiton A component is a bundle of code that helps solve a specific problem

Slide 94

Slide 94 text

Component oriented development definiton A component is a bundle of code that helps solve a specific problem Good components are :

Slide 95

Slide 95 text

Component oriented development definiton A component is a bundle of code that helps solve a specific problem Good components are : • Laser-focused • Small • Cooperative • Well-tested • Well-documented

Slide 96

Slide 96 text

Component oriented development definiton A component is a bundle of code that helps solve a specific problem Good components are : • Laser-focused • Small • Cooperative • Well-tested • Well-documented %QORQPGPVUXU(TCOGYQTMU

Slide 97

Slide 97 text

Components Where to find them? https://packagist.org Find components :

Slide 98

Slide 98 text

Components Where to find them? http://packalyst.com/ Laravel-only components :

Slide 99

Slide 99 text

Components How to install them?

Slide 100

Slide 100 text

Composer definition Take care of package download and autoload

Slide 101

Slide 101 text

Composer definition Works hand-in-hand with Packagist Take care of package download and autoload

Slide 102

Slide 102 text

Composer definition Works hand-in-hand with Packagist Take care of package download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG

Slide 103

Slide 103 text

Composer definition Works hand-in-hand with Packagist Take care of package download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Composer

Slide 104

Slide 104 text

Composer definition Works hand-in-hand with Packagist Take care of package download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Packagist HGVEJGUVJGRCEMCIG Composer

Slide 105

Slide 105 text

Composer definition Works hand-in-hand with Packagist Take care of package download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Packagist HGVEJGUVJGRCEMCIG Composer GitHub HKPFUVJGTGRQ74.

Slide 106

Slide 106 text

Composer definition Works hand-in-hand with Packagist Take care of package download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Packagist HGVEJGUVJGRCEMCIG Composer GitHub HKPFUVJGTGRQ74. FGVGTOKPGURCEMCIG XGTUKQP

Slide 107

Slide 107 text

Composer definition Works hand-in-hand with Packagist Take care of package download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Packagist HGVEJGUVJGRCEMCIG Composer GitHub HKPFUVJGTGRQ74. FGVGTOKPGURCEMCIG XGTUKQP Project FQYPNQCFUVJGRCEMCIG CPFKVUFGRGPFGPEKGU

Slide 108

Slide 108 text

guzzlehttp/guzzle Composer usage

Slide 109

Slide 109 text

guzzlehttp/guzzle Composer usage CWVJQTPCOG

Slide 110

Slide 110 text

guzzlehttp/guzzle Composer usage CWVJQTPCOG RCEMCIGPCOG

Slide 111

Slide 111 text

Composer composer.json { "name": "zianwar/fancyapp", "description": "My fancy app", "keywords": ["morocco", "fancy", "hh"], "homepage": "http://fancyapp.com", "license": "MIT", "authors": [ { "name": "Anwar Ziani", "homepage": "https://github.com/zianwar", "role": "Artisan" } ], "support": { "email": "[email protected]" }, "require": { "php" : ">=5.4.0", "guzzlehttp/guzzle": "~5.0" }, "require-dev": { “phpunit/phpunit": "~4.3" }, "suggest": { "league/flysystem": "~1.0" }, "autoload": { "psr-4": { "FancyApp\\": "src/" } } }

Slide 112

Slide 112 text

Composer composer.json { "name": "zianwar/fancyapp", "description": "My fancy app", "keywords": ["morocco", "fancy", "hh"], "homepage": "http://fancyapp.com", "license": "MIT", "authors": [ { "name": "Anwar Ziani", "homepage": "https://github.com/zianwar", "role": "Artisan" } ], "support": { "email": "[email protected]" }, "require": { "php" : ">=5.4.0", "guzzlehttp/guzzle": "~5.0" }, "require-dev": { “phpunit/phpunit": "~4.3" }, "suggest": { "league/flysystem": "~1.0" }, "autoload": { "psr-4": { "FancyApp\\": "src/" } } } getcomposer.org . NGCTPOQTG CDQWVEQORQUGTCV

Slide 113

Slide 113 text

Composer composer.json Laravel’s composer.json file :

Slide 114

Slide 114 text

Composer composer.json

Slide 115

Slide 115 text

Resources

Slide 116

Slide 116 text

Resources Modern PHP New Features and Good Practices - By Josh Lockhart

Slide 117

Slide 117 text

Resources Modern PHP New Features and Good Practices - By Josh Lockhart https://github.com/ziadoz/awesome-php A curated list of amazingly awesome PHP libraries, resources and shiny things.

Slide 118

Slide 118 text

Resources Modern PHP New Features and Good Practices - By Josh Lockhart https://github.com/ziadoz/awesome-php A curated list of amazingly awesome PHP libraries, resources and shiny things. https://github.com/phptodayorg/php-must-watch List of interesting conference talks and videos on PHP

Slide 119

Slide 119 text

Resources Modern PHP New Features and Good Practices - By Josh Lockhart https://github.com/ziadoz/awesome-php A curated list of amazingly awesome PHP libraries, resources and shiny things. https://github.com/phptodayorg/php-must-watch List of interesting conference talks and videos on PHP https://laracasts.com/ The best PHP screencasts on the web

Slide 120

Slide 120 text

No content

Slide 121

Slide 121 text

Thank you