Webinar Darkmira PHParty7 France - Présentation des avantages de la nouvelle fonctionnalité de php7, le support des types scalaires dans la définition des arguments d'une fonction
2. Historique
• PHP 5.0
Support des types non scalaires liés à la POO (Classes, Interfaces,
mot-clé self)
• PHP 5.1
Support des tableaux (mot-clé array)
• PHP 5.4
Support des fonctions de rappel (type Callable)
• PHP 7
Support des types scalaires (string, int, float, bool)
4. Rappels - Classes
Affiche :
PHP7
Fatal error: Uncaught TypeError: Argument 1
passed to printList() must be an instance of
ArrayObject, instance of ArrayIterator given, called
in ... on line 10 and defined in ...:2
6. Rappels - Interfaces
Affiche :
PHP7
PHP7
Fatal error: Uncaught TypeError: Argument 1
passed to printList() must implement interface
ArrayAccess, array given, called in ... on line 10
and defined in ...:2
8. Rappels - Tableaux
Affiche :
PHP7
Fatal error: Uncaught TypeError: Argument 1
passed to printList() must be of the type array,
object given, called in ... on line 9 and defined in
...:2
10. Rappels - Fonctions de rappel
Affiche :
PHP7
Aucune sortie
Fatal error: Uncaught TypeError: Argument 2
passed to printList() must be callable, string given,
called in ... on line 11 and defined in ...:2
12. PHP7 Scalar Type Hint
• Types scalaires (primitifs)
• Chaînes de caractères (mot-clé string)
• Nombres décimaux entiers (mot-clé int)
• Nombres décimaux à virgules flottantes (float)
• Booléens (bool)
13. PHP7 Scalar Type Hint
• Types scalaires (primitifs)
• Chaînes de caractères (mot-clé string)
• Nombres décimaux entiers (mot-clé int)
• Nombres décimaux à virgules flottantes (float)
• Booléens (bool)
• Désactivé par défaut
14. PHP7 Scalar Type Hint
• Types scalaires (primitifs)
• Chaînes de caractères (mot-clé string)
• Nombres décimaux entiers (mot-clé int)
• Nombres décimaux à virgules flottantes (float)
• Booléens (bool)
• Désactivé par défaut
• Activé à l’aide de l’instruction declare(strict_types=1); en
début de fichier.
15. PHP7 Scalar Type Hint
• Types scalaires (primitifs)
• Chaînes de caractères (mot-clé string)
• Nombres décimaux entiers (mot-clé int)
• Nombres décimaux à virgules flottantes (float)
• Booléens (bool)
• Désactivé par défaut
• Activé à l’aide de l’instruction declare(strict_types=1); en
début de fichier.
• Activé uniquement sur le fichier ou l’instruction est
présente.
16. PHP7 Scalar Type Hint
• Types scalaires (primitifs)
• Chaînes de caractères (mot-clé string)
• Nombres décimaux entiers (mot-clé int)
• Nombres décimaux à virgules flottantes (float)
• Booléens (bool)
• Désactivé par défaut
• Activé à l’aide de l’instruction declare(strict_types=1); en
début de fichier.
• Activé uniquement sur le fichier ou l’instruction est
présente.
• Déclaration : function a(int $a) {...}
17. PHP7 Scalar Type Hint
• Types scalaires (primitifs)
• Chaînes de caractères (mot-clé string)
• Nombres décimaux entiers (mot-clé int)
• Nombres décimaux à virgules flottantes (float)
• Booléens (bool)
• Désactivé par défaut
• Activé à l’aide de l’instruction declare(strict_types=1); en
début de fichier.
• Activé uniquement sur le fichier ou l’instruction est
présente.
• Déclaration : function a(int $a) {...}
• Exception dédiée : TypeError
19. Scalar Type Hint - strict type
Affiche :
3
Fatal error: Uncaught TypeError: Argument 1
passed to sum() must be of the type integer, float
given, called in ... on line 9 and defined in ...:4
22. Scalar Type Hint - strict type -
string exemple
Affiche :
PHP 7
Fatal error: Uncaught TypeError: Argument 2
passed to concat() must be of the type string,
integer given, called in ... on line 9 and defined in
...:4
24. Scalar Type Hint - arguments
variables
Affiche :
10
Fatal error: Uncaught TypeError: Argument 4
passed to sum() must be of the type integer,
string given, called in ... on line 9 and defined in
...:4
26. Scalar Type Hint - exception
Affiche :
Erreur de type : Argument 1 passed to sum() must
be of the type integer,
float given, called in ... on line 9
28. Scalar Type Hint - null type
Affiche :
Fatal error: Uncaught TypeError: Argument 1
passed to returnAsItIs() must be of the type
integer, null given, called in ... on line 12 and
defined in ...:4
31. Avantages
• Optionnel (typage faible par défaut)
• Moins de code
• Moins de documentation
• Code plus robuste
• force le développeur à identifier les E/S de ses
fonctions/méthodes
• permet à l’interpréteur de repérer les
dysfonctionnements avant l’utilisateur
• permet de consommer un service en toute
sérénité
35. Avantages - code plus
robuste (1-2)
Affiche :
Notice: A non well formed numeric value
encountered in ... on line 2
4
Notice: A non well formed numeric value
encountered in ... on line 2
4
36. Avantages - code plus
robuste (1-3)
Affiche :
Fatal error: Uncaught TypeError: Argument 2
passed to sum() must be of the type integer,
string given, called in ... on line 8 and defined in
...:4
40. Autre avantages...
• Sécurité accrue (filtrer les données entrantes,
échapper les données sortantes !)
• Écriture des tests simplifiée (moins d’assertion,
moins de test)
• Gain de performance (compilation en Opcode plus
facile)