Download - Php 5.3
![Page 1: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/1.jpg)
Intervenant: Benoît DURAND b.durand at epixelic dot com
PHP 5.3Les namespaces, et les closures
05/10/2011 Développement
![Page 2: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/2.jpg)
Les espaces de noms(namespaces)
![Page 3: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/3.jpg)
3
La portée
1. Fonctions2. Classes3. Constantes
05/10/2011 PHP 5.3
![Page 4: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/4.jpg)
4
La déclaration
Le mot clé est :namespace
Le séparateur est : \
05/10/2011 PHP 5.3
![Page 5: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/5.jpg)
5
La déclaration
<?phpnamespace Epx;
function number_format($number) { return \number_format($number, 2, ',', ' ');}
echo number_format(2); // Affiche 2,00
05/10/2011 PHP 5.3
![Page 6: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/6.jpg)
6
Les sous espaces de noms
Le séparateur d’espaces de noms permet de définir un sous espace de nom :
namespace Epx\Payment;
05/10/2011 PHP 5.3
![Page 7: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/7.jpg)
7
L’ordre de résolution
1. Un nom qualifié
2. L’espace de nom courant
3. L’espace de nom global
05/10/2011 PHP 5.3
![Page 8: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/8.jpg)
8
L’ordre de résolution
1. Un nom qualifiéException\RuntimeException
2. L’espace de nom courantRuntimeException
3. L’espace de nom global\RuntimeException
05/10/2011 PHP 5.3
![Page 9: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/9.jpg)
9
Utilisation des espaces de noms
// Fonctionecho number_format(2); // Affiche 2echo Epx\number_format(2); // Affiche 2,00
// Classe$e = new \Exception('error');
// Constanteconst APPLICATION_ENV = 'cli'; // Nouveauté 5.3echo APPLICATION_ENV; // Affiche cli05/10/2011 PHP 5.3
![Page 10: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/10.jpg)
10
Import
Le mot clé est :
use
05/10/2011 PHP 5.3
![Page 11: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/11.jpg)
11
Import
namespace Epx;
use Zend\Registry, // import de la classe Zend\Translator; // import de l’espace de nom
$r = new Registry;$t = new Translator\Translator;
05/10/2011 PHP 5.3
![Page 12: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/12.jpg)
12
Alias
Le mot clé est : as
05/10/2011 PHP 5.3
![Page 13: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/13.jpg)
13
Alias
namespace Application;use Zend\Controller\Action as ActionController;
class IndexController extends ActionController {}
05/10/2011 PHP 5.3
![Page 14: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/14.jpg)
14
Vous êtes perdus ?
• Nouvelle constante magique__NAMESPACE__
05/10/2011 PHP 5.3
![Page 15: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/15.jpg)
15
Etude de cas
// Aujourd’hui, ZF1 est en style PHP 5.2require_once 'Zend_Controller_Action';
class IndexController extends Zend_Controller_Action {}
05/10/2011 PHP 5.3
![Page 16: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/16.jpg)
16
Etude de cas : migration d’une librairie
// Import du contrôleur ZF1use Zend_Controller_Action as ActionController;class IndexController extends ActionController {}
// Après la migration ZF2use Zend\Controller\Action as ActionController;class IndexController extends ActionController {}
05/10/2011 PHP 5.3
![Page 17: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/17.jpg)
17
Lisibilité du code
// Test sur un type d’objetnamespace Zend\Log;if ($writer instanceof Writer) {/*…*/}
// Type d’une classenamespace Zend\Log\Writer;use Zend\Log\Writer;class Mail implements Writer {}
05/10/2011 PHP 5.3
![Page 18: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/18.jpg)
18
Lisibilité du code
// Notation ZF1 compatible PHP 5.2$vr = new Zend_Controller_Action_Helper_ViewRenderer;Zend_Controller_Action_HelperBroker::addHelper($vr);
// Notation raccourcieuse Zend\Controller\Action\HelperBroker, Zend\Controller\Action\Helper\ViewRenderer;HelperBroker::addHelper(new ViewRenderer);
05/10/2011 PHP 5.3
![Page 19: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/19.jpg)
19
PSR-0 : PHP Specification Request 0
• Interopérabilité pour l’autoload entre les frameworks
• Zend Framework 2, Doctrine2, Symfony2 l’ont déjà adopté
05/10/2011 PHP 5.3
![Page 20: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/20.jpg)
20
PSR-0 : exemples
• Zend\Mail\Message=> /vendor/Zend/Mail/Message.php• Doctrine\ORM\EntityManager=> /vendor/Doctrine/ORM/EntityManager.php• Symfony\Component\Yaml\Parser=> /vendor/Symfony/Component/Yaml/Parser.php
05/10/2011 PHP 5.3
![Page 21: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/21.jpg)
21
Résumons
• Améliorer la lecture avec des noms courts
05/10/2011 PHP 5.3
![Page 22: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/22.jpg)
22
Résumons
• Améliorer la lecture avec des noms courts• Refactoriser devient simple
05/10/2011 PHP 5.3
![Page 23: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/23.jpg)
23
Résumons
• Améliorer la lecture avec des noms courts• Refactoriser devient simple• Chaque classe définit un espace de nom
05/10/2011 PHP 5.3
![Page 24: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/24.jpg)
24
Résumons
• Améliorer la lecture avec des noms courts• Refactoriser devient simple• Chaque classe définit un espace de nom• Un espace de nom par fichier
05/10/2011 PHP 5.3
![Page 25: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/25.jpg)
25
Résumons
• Améliorer la lecture avec des noms courts• Refactoriser devient simple• Chaque classe définit un espace de nom• Un espace de nom par fichier• Utiliser un espace de nom unique
05/10/2011 PHP 5.3
![Page 26: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/26.jpg)
26
Résumons
• Améliorer la lecture avec des noms courts• Refactoriser devient simple• Chaque classe définit un espace de nom• Un espace de nom par fichier• Utiliser un espace de nom unique• Utiliser l’import au lieu de require_once
05/10/2011 PHP 5.3
![Page 27: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/27.jpg)
27
Résumons
• Améliorer la lecture avec des noms courts• Refactoriser devient simple• Chaque classe définit un espace de nom• Un espace de nom par fichier• Utiliser un espace de nom unique• Utiliser l’import au lieu de require_once• Eviter d’utiliser la résolution globale (sauf pour
les chaînes de caractères)
05/10/2011 PHP 5.3
![Page 28: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/28.jpg)
Les fonctions anonymes(closures)
![Page 29: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/29.jpg)
29
Fonctions anonymes
// PHP 5.2$pages = array(…);$cmp_function = create_function( '$a,$b', 'return strcmp($a["label"], $b["label"]); ');usort($pages, $cmp_function); var_dump($pages);
05/10/2011 PHP 5.3
![Page 30: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/30.jpg)
30
Fonctions de rappel (callback)
$pages = array( array('label' => 'home'), array('label' => 'about'),);// C’est plus rapide que create_function()usort($pages, function ($a, $b) { return strcmp($a['label'], $b['label']);});
05/10/2011 PHP 5.3
![Page 31: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/31.jpg)
31
Contexte et variables
$logger = new Logger;$log = function ($msg) use ($logger) { if (is_array($msg)) { $msg = json_encode($msg); } $logger->crit((string) $msg);};$log('il neige');
05/10/2011 PHP 5.3
![Page 32: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/32.jpg)
Bonus
![Page 33: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/33.jpg)
33
Amélioration des performances
• Drupal : 20% plus rapide• Wordpress : 15% plus rapide
source : http://news.php.net/php.internals/36484
05/10/2011 PHP 5.3
![Page 34: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/34.jpg)
34
Nouvelle méthode magique
namespace My\Filter;class Int{ public function __invoke($value) { return filter_var($value, FILTER_SANITIZE_NUMBER_INT); }}$filter = new Int;var_dump($filter('CL1234')); // Affiche 1234
05/10/2011 PHP 5.3
![Page 35: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/35.jpg)
35
Les limitations du mot clé self
namespace Cwp\Boutique;class Commande{ public static function get($id) { return new self; }}
use Cwp\BoutiqueEvolution\Commande;$cl = get_class(Commande::get(42));var_dump($cl); // Affiche Cwp\Boutique\Commande05/10/2011 PHP 5.3
![Page 36: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/36.jpg)
36
Late State Binding
Le mot clé est :
static
05/10/2011 PHP 5.3
![Page 37: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/37.jpg)
37
Late State Binding
namespace Cwp\Boutique;class Commande{ public static function get($id) { return new static; }}
use Cwp\BoutiqueEvolution\Commande;$cl = get_class(Commande::get(42));var_dump($cl); // Affiche Cwp\BoutiqueEvolution\Commande05/10/2011 PHP 5.3
![Page 38: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/38.jpg)
38
Autres nouveautés
• Nouvelle constante magique__DIR__ === dirname(__FILE__)
05/10/2011 PHP 5.3
![Page 39: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/39.jpg)
39
Autres nouveautés
• Nouvelle constante magique__DIR__ === dirname(__FILE__)• Nouvelle méthode magique__callStatic()
05/10/2011 PHP 5.3
![Page 40: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/40.jpg)
40
Autres nouveautés
• Nouvelle constante magique__DIR__ === dirname(__FILE__)• Nouvelle méthode magique__callStatic()• Phar (archive PHP)
05/10/2011 PHP 5.3
![Page 41: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/41.jpg)
41
Autres nouveautés
• Nouvelle constante magique__DIR__ === dirname(__FILE__)• Nouvelle méthode magique__callStatic()• Phar (archive PHP)• Ajout de NOWDOC
05/10/2011 PHP 5.3
![Page 42: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/42.jpg)
42
Autres nouveautés
• Nouvelle constante magique__DIR__ === dirname(__FILE__)• Nouvelle méthode magique__callStatic()• Phar (archive PHP)• Ajout de NOWDOC• Ajout d’un garbage collector
05/10/2011 PHP 5.3
![Page 43: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/43.jpg)
43
Autres nouveautés
• Nouvelle constante magique__DIR__ === dirname(__FILE__)• Nouvelle méthode magique__callStatic()• Phar (archive PHP)• Ajout de NOWDOC• Ajout d’un garbage collector• Support amélioré pour Windows
05/10/2011 PHP 5.3
![Page 44: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/44.jpg)
Git 44
Documentation
• http://php.net/namespace• http://tinyurl.com/fig-psr0• http://php.net/manual/fr/functions.anonymous.php• http
://php.net/manual/fr/language.oop5.late-static-bindings.php
• http://php.net/manual/fr/migration53.new-features.php
11/04/2011
![Page 45: Php 5.3](https://reader035.vdocuments.fr/reader035/viewer/2022070320/5586ea46d8b42a04138b4707/html5/thumbnails/45.jpg)
PHP 5.3 45
Questions ?
05/10/2011