Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Modern PHP

Modern PHP

A Quick overview of some modern PHP features, such as namespaces, traits, and closures ... and composer as a dependecy manager.

Anwar Ziani

March 14, 2015
Tweet

More Decks by Anwar Ziani

Other Decks in Programming

Transcript

  1. 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
  2. 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
  3. PHP was created by Rasmus Lerdorf, as a collection of

    CGI scripts to track visits to his online resume. How it started
  4. 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”
  5. 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
  6. 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
  7. PHP is so popular, supported by many developers and companies.

    (Wikipedia, Facebook, Yahoo!…) The present of PHP
  8. 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
  9. 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…
  10. 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…
  11. Namespaces definition Important tool to organise PHP code into a

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

    virtual hierarchy +PVTQFWEGFKP 2*2 Just like operating system’s filesystem directory structure
  13. 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
  14. Namespaces symfony/httpfoundation Symfony framework’s component that manages HTTP requests and

    responses : class Request class Response %QORQPGPV definition
  15. <?php namespace ENSAT\Registration; <?php namespace ENSAT\Mailing; <?php namespace ENSAT; //

    a class, interface, or function … global namespace 5JQWNFDGWPKSWG Namespaces declaration
  16. <?php namespace ENSAT\Registration; <?php namespace ENSAT\Mailing; <?php namespace ENSAT; //

    a class, interface, or function … subnamespaces global namespace 5JQWNFDGWPKSWG Namespaces declaration
  17. Namespaces declaration <?php namespace ENSAT\Mailing; class StudentsMailer { } StudentMailer.php

    <?php namespace ENSAT\Mailing; class TeachersMailer { } TeachersMailer.php
  18. Namespaces declaration <?php namespace ENSAT\Mailing; class StudentsMailer { } StudentMailer.php

    <?php namespace ENSAT\Mailing; class SomeClass { } file.php <?php namespace ENSAT\Mailing; class TeachersMailer { } TeachersMailer.php
  19. Namespaces declaration <?php namespace ENSAT\Mailing; class StudentsMailer { } StudentMailer.php

    <?php namespace ENSAT\Mailing; class SomeClass { } file.php <?php namespace ENSAT\Mailing; class TeachersMailer { } TeachersMailer.php ENSAT\Mailing { TeacherMailer StudentsMailer SomeClass
  20. Namespaces use <?php $request = new Symfony\Component\Httpfoundation\Request; <?php use Symfony\Component\Httpfoundation\Request;

    use ENSAT\Routing\Response; $request = new Request; $response = new Response; $response = new ENSAT\Routing\Response;
  21. <?php use func ENSAT\functionName; functionName(); Namespaces use <?php $request =

    new Symfony\Component\Httpfoundation\Request; <?php use Symfony\Component\Httpfoundation\Request; use ENSAT\Routing\Response; $request = new Request; $response = new Response; $response = new ENSAT\Routing\Response;
  22. <?php use func ENSAT\functionName; functionName(); Namespaces use <?php $request =

    new Symfony\Component\Httpfoundation\Request; <?php use Symfony\Component\Httpfoundation\Request; use ENSAT\Routing\Response; $request = new Request; $response = new Response; $response = new ENSAT\Routing\Response; <?php use constant ENSAT\CONT_NAME; echo CONST_NAME;
  23. <?php use func ENSAT\functionName; functionName(); Namespaces use <?php $request =

    new Symfony\Component\Httpfoundation\Request; <?php use Symfony\Component\Httpfoundation\Request; use ENSAT\Routing\Response; $request = new Request; $response = new Response; $response = new ENSAT\Routing\Response; <?php use constant ENSAT\CONT_NAME; echo CONST_NAME; 2*2
  24. Namespaces alias <?php $coolClass = new Acme\Subnamespace\AbstractSingletonProxyFactoryBean; <?php use Acme\Subnamespace\AbstractSingletonProxyFactoryBean;

    $coolClass = new AbstractSingletonProxyFactoryBean; <?php use Acme\Subnamespace\AbstractSingletonProxyFactoryBean as TheCoolClass; $coolClass = new TheCoolClass;
  25. Namespaces default namespace <?php namespace Foo\Subnamespace; $e = new Exception;

    '4414ENCUUFQGU PQVGZKUVU <?php namespace Foo\Subnamespace; $e = new \Exception;
  26. Namespaces default namespace <?php namespace Foo\Subnamespace; $e = new Exception;

    '4414ENCUUFQGU PQVGZKUVU <?php namespace Foo\Subnamespace; $e = new \Exception;
  27. Traits definition +PVTQFWEGFKP 2*2 • Partial class implementation • Look

    like an interface, behave like a class • Cannot be instantiated on it’s own
  28. Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class

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

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

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

    Foo1 extends AbstractFoo class AbstractBar class Bar2 extends AbstractBar class Bar1 extends AbstractBar Shareable, Likeable,
  32. 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 …
  33. Traits declaration <?php trait MyTrait { // Trait implementation }

    <?php trait Shareable { public function share() { return “Shared!”; } }
  34. Traits declaration <?php trait MyTrait { // Trait implementation }

    <?php trait Shareable { public function share() { return “Shared!”; } } <?php class Foo1 { use Shareable; … }
  35. Traits declaration <?php trait MyTrait { // Trait implementation }

    <?php trait Shareable { public function share() { return “Shared!”; } } <?php class Foo1 { use Shareable; … } $foo = new Foo1; $foo->share();
  36. 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.
  37. 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
  38. Closures examples <?php $closure = function ($name) { return echo

    'Hello $name'; }; $closure(‘Anwar’); // Hello Anwar
  39. Closures examples <?php $closure = function ($name) { return echo

    'Hello $name'; }; $closure(‘Anwar’); // Hello Anwar <?php $numbers = array_map(function ($number) { return $number + 1; }, [1,2,3]); print_r($numbers); // [2,3,4]
  40. Closures examples <?php $closure = function ($name) { return echo

    'Hello $name'; }; $closure(‘Anwar’); // Hello Anwar <?php $numbers = array_map(function ($number) { return $number + 1; }, [1,2,3]); print_r($numbers); // [2,3,4] array_map — Applies the callback to the elements of the given arrays array array_map(callable $callback , array $array1 [, array $... ] )
  41. Closures examples <?php function enclosePerson($name) { return function ($doCommand) use

    ($name) { return sprintf('%s, %s', $name, $doCommand); }; } // Enclose “Mustapha" string in closure $person = enclosePerson('Mustapha'); // Invoke closure with command echo $person('get me some coffee!'); // Outputs --> "Mustapha, get me some coffee!” Attaching state to closures :
  42. Closures examples Examples of Closures in Laravel framework : <?php

    Route::get('/', function() { return ‘Hello World’; });
  43. Closures examples Examples of Closures in Laravel framework : <?php

    Route::get('/', function() { return ‘Hello World’; }); <?php Event::listen(‘SomeEvent', function($event) { // Handle the event … });
  44. 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
  45. 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.”
  46. Standards PSR • PSR-1 : Basic code style • PSR-2

    : Strict code style • PSR-3 : Logger interface • PSR-4 : Autoloading PHP standards recommendations
  47. Standards PSR PSR-4 : Autoloader : Autoloading classes from file

    paths <?php include ‘path/to/file1.php’; include ‘path/to/file2.php’; require_once ‘path/to/file3.php’; 0QOQTG
  48. Standards PSR PSR-4 Autoloader Strategy : Mapping the top-level namespace

    prefix to a specific filesystem directory Acme\ src/ PCOGURCEG FKTGEVQT[
  49. 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
  50. Standards PSR … "autoload": { "classmap": [ "database" ], },

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

    … composer.json Laravel is using PSR-4 : "psr-4": { "App\\": "app/" }
  52. Component oriented development definiton A component is a bundle of

    code that helps solve a specific problem Good components are :
  53. 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
  54. 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
  55. Composer definition Works hand-in-hand with Packagist Take care of package

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

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

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

    download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Packagist HGVEJGUVJGRCEMCIG Composer GitHub HKPFUVJGTGRQ74.
  59. 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
  60. 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
  61. 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/" } } }
  62. 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
  63. 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.
  64. 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
  65. 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