Share
Embed
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
Transcript
121 slides
-
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 -
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 -
PHP was created by Rasmus Lerdorf, as a collection of
CGI scripts to track visits to his online resume. How it started -
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” -
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 -
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 -
PHP is so popular, supported by many developers and companies.
(Wikipedia, Facebook, Yahoo!…) The present of PHP -
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 -
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… -
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… -
Namespaces definition Important tool to organise PHP code into a
virtual hierarchy +PVTQFWEGFKP 2*2 -
Namespaces definition Important tool to organise PHP code into a
virtual hierarchy +PVTQFWEGFKP 2*2 Just like operating system’s filesystem directory structure -
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 -
Namespaces symfony/httpfoundation Symfony framework’s component that manages HTTP requests and
responses : %QORQPGPV definition -
Namespaces symfony/httpfoundation Symfony framework’s component that manages HTTP requests and
responses : class Request class Response %QORQPGPV definition -
Symfony\Component\Httpfoundation ENSAT\Routing class Request class Response class AnotherClass ... {
class Request class Response ... { Namespaces why ? -
Symfony\Component\Httpfoundation ENSAT\Routing class Request class Response class AnotherClass ... {
class Request class Response ... { Namespaces why ? '4414 -
<?php namespace ENSAT; // a class, interface, or function …
Namespaces declaration -
<?php namespace ENSAT\Registration; <?php namespace ENSAT; // a class, interface,
or function … Namespaces declaration -
<?php namespace ENSAT\Registration; <?php namespace ENSAT\Mailing; <?php namespace ENSAT; //
a class, interface, or function … Namespaces declaration -
<?php namespace ENSAT\Registration; <?php namespace ENSAT\Mailing; <?php namespace ENSAT; //
a class, interface, or function … global namespace 5JQWNFDGWPKSWG Namespaces declaration -
<?php namespace ENSAT\Registration; <?php namespace ENSAT\Mailing; <?php namespace ENSAT; //
a class, interface, or function … subnamespaces global namespace 5JQWNFDGWPKSWG Namespaces declaration -
Namespaces declaration <?php namespace ENSAT\Mailing; class StudentsMailer { } StudentMailer.php
<?php namespace ENSAT\Mailing; class TeachersMailer { } TeachersMailer.php -
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 -
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 -
Namespaces use <?php $request = new Symfony\Component\Httpfoundation\Request; $response = new
ENSAT\Routing\Response; -
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 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 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; -
<?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 -
Namespaces alias <?php $coolClass = new Acme\Subnamespace\AbstractSingletonProxyFactoryBean; <?php use Acme\Subnamespace\AbstractSingletonProxyFactoryBean;
$coolClass = new AbstractSingletonProxyFactoryBean; -
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; -
Namespaces default namespace <?php namespace Foo\Subnamespace; $e = new Exception;
'4414ENCUUFQGU PQVGZKUVU -
Namespaces default namespace <?php namespace Foo\Subnamespace; $e = new Exception;
'4414ENCUUFQGU PQVGZKUVU <?php namespace Foo\Subnamespace; $e = new \Exception; -
Namespaces default namespace <?php namespace Foo\Subnamespace; $e = new Exception;
'4414ENCUUFQGU PQVGZKUVU <?php namespace Foo\Subnamespace; $e = new \Exception; -
Traits definition +PVTQFWEGFKP 2*2 • Partial class implementation • Look
like an interface, behave like a class • Cannot be instantiated on it’s own -
Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class
Foo1 extends AbstractFoo -
Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class
Foo1 extends AbstractFoo class AbstractBar -
Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class
Foo1 extends AbstractFoo class AbstractBar class Bar2 extends AbstractBar class Bar1 extends AbstractBar -
Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class
Foo1 extends AbstractFoo class AbstractBar class Bar2 extends AbstractBar class Bar1 extends AbstractBar Shareable, -
Traits why ? class AbstractFoo class Foo2 extends AbstractFoo class
Foo1 extends AbstractFoo class AbstractBar class Bar2 extends AbstractBar class Bar1 extends AbstractBar Shareable, Likeable, -
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 … -
Traits declaration <?php trait MyTrait { // Trait implementation }
<?php trait Shareable { public function share() { return “Shared!”; } } -
Traits declaration <?php trait MyTrait { // Trait implementation }
<?php trait Shareable { public function share() { return “Shared!”; } } <?php class Foo1 { use Shareable; … } -
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(); -
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. -
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 -
Closures examples <?php $closure = function ($name) { return echo
'Hello $name'; }; $closure(‘Anwar’); // Hello Anwar -
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] -
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 $... ] ) -
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 : -
Closures examples Examples of Closures in Laravel framework : <?php
Route::get('/', function() { return ‘Hello World’; }); -
Closures examples Examples of Closures in Laravel framework : <?php
Route::get('/', function() { return ‘Hello World’; }); <?php Event::listen(‘SomeEvent', function($event) { // Handle the event … }); -
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 -
Built-in HTTP server usage zianwar$ php -S localhost:4000 In terminal,
navigate to your project root and type : -
Built-in HTTP server usage zianwar$ php -S localhost:4000 In terminal,
navigate to your project root and type : -
Standards intro Laravel Symfony Silex Slim CodeIgniter } } /CETQHTCOGYQTMU
/KETQHTCOGYQTMU .GICE[HTCOGYQTM -
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.” -
Standards intro • Interfaces • Autoloading • Style Modern framework
share different standards : -
Standards PSR • PSR-1 : Basic code style • PSR-2
: Strict code style • PSR-3 : Logger interface • PSR-4 : Autoloading PHP standards recommendations -
Standards PSR PSR-1 : Basic Code Style : 'CU[VQ KORNGOGPV
http://www.php-fig.org/psr/psr-1/ -
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 -
Standards PSR PSR-4 Autoloader Strategy : Mapping the top-level namespace
prefix to a specific filesystem directory -
Standards PSR PSR-4 Autoloader Strategy : Mapping the top-level namespace
prefix to a specific filesystem directory Acme\ src/ PCOGURCEG FKTGEVQT[ -
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 -
Standards PSR <?php namespace Acme\Something class A { } src/
|-- Something/ |-- A.php -
Standards PSR <?php namespace Acme\Something class A { } src/
|-- Something/ |-- A.php -
Standards PSR … "autoload": { "classmap": [ "database" ], },
… composer.json Laravel is using PSR-4 : "psr-4": { "App\\": "app/" } -
Standards PSR … "autoload": { "classmap": [ "database" ], },
… composer.json Laravel is using PSR-4 : "psr-4": { "App\\": "app/" } -
Component oriented development definiton A component is a bundle of
code that helps solve a specific problem -
Component oriented development definiton A component is a bundle of
code that helps solve a specific problem Good components are : -
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 -
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 -
Composer definition Works hand-in-hand with Packagist Take care of package
download and autoload -
Composer definition Works hand-in-hand with Packagist Take care of package
download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG -
Composer definition Works hand-in-hand with Packagist Take care of package
download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Composer -
Composer definition Works hand-in-hand with Packagist Take care of package
download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Packagist HGVEJGUVJGRCEMCIG Composer -
Composer definition Works hand-in-hand with Packagist Take care of package
download and autoload +YCPVVJG guzzlehttp/guzzle RCEMCIG Packagist HGVEJGUVJGRCEMCIG Composer GitHub HKPFUVJGTGRQ74. -
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 -
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 -
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/" } } } -
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 -
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. -
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 -
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