Upload
darkmira
View
190
Download
2
Embed Size (px)
Citation preview
Scalar Types HintsWebinar PHParty7 France - 28/11/15
Historique
• PHP 5.0Support des types non scalaires liés à la POO (Classes, Interfaces, mot-clé self)
• PHP 5.1Support des tableaux (mot-clé array)
• PHP 5.4Support des fonctions de rappel (type Callable)
• PHP 7Support des types scalaires (string, int, float, bool)
Rappels - Classes
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
Rappels - Interfaces
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
Rappels - Tableaux
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
Rappels - Fonctions de rappel
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
PHP7 Scalar Type Hint
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)
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
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.
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.
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) {...}
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
Scalar Type Hint - strict type
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
Scalar Type Hint - weak type
Scalar Type Hint - weak type
Affiche :
3
2
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
Scalar Type Hint - arguments variables
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
Scalar Type Hint - exception
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
Scalar Type Hint - null type
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
Scalar Type Hint - effet local
Scalar Type Hint - effet local
Affiche :
3
2
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é
Avantages - moins de code, moins de doc (1)
Avantages - moins de code, moins de doc (2)
Avantages - code plus robuste (1-1)
Affiche :
4
4
Avantages - code plus robuste (1-2)
Affiche :
Notice: A non well formed numeric value encountered in ... on line 24Notice: A non well formed numeric value encountered in ... on line 24
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
Avantages - code plus robuste (2-1)
Avantages - code plus robuste (2-2)
Avantages - code plus robuste (2-2)
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)
Avantages - tests unitaires simplifiés
Avantages - tests unitaires simplifiés
Avantages - tests unitaires simplifiés
Avantages - tests unitaires simplifiés
Avantages - tests unitaires simplifiés
Avantages - tests unitaires simplifiés
Des questions ?