Top Banner
Scalar Types Hints Webinar PHParty7 France - 28/11/15
47

Scalar Types Hints

Apr 12, 2017

Download

Internet

Darkmira
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Scalar Types Hints

Scalar Types HintsWebinar PHParty7 France - 28/11/15

Page 2: Scalar Types Hints

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)

Page 3: Scalar Types Hints

Rappels - Classes

Page 4: Scalar Types Hints

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

Page 5: Scalar Types Hints

Rappels - Interfaces

Page 6: Scalar Types Hints

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

Page 7: Scalar Types Hints

Rappels - Tableaux

Page 8: Scalar Types Hints

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

Page 9: Scalar Types Hints

Rappels - Fonctions de rappel

Page 10: Scalar Types Hints

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

Page 11: Scalar Types Hints

PHP7 Scalar Type Hint

Page 12: Scalar Types Hints

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)

Page 13: Scalar Types Hints

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

Page 14: Scalar Types Hints

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.

Page 15: Scalar Types Hints

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.

Page 16: Scalar Types Hints

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) {...}

Page 17: Scalar Types Hints

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

Page 18: Scalar Types Hints

Scalar Type Hint - strict type

Page 19: Scalar Types Hints

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

Page 20: Scalar Types Hints

Scalar Type Hint - weak type

Page 21: Scalar Types Hints

Scalar Type Hint - weak type

Affiche :

3

2

Page 22: Scalar Types Hints

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

Page 23: Scalar Types Hints

Scalar Type Hint - arguments variables

Page 24: Scalar Types Hints

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

Page 25: Scalar Types Hints

Scalar Type Hint - exception

Page 26: Scalar Types Hints

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

Page 27: Scalar Types Hints

Scalar Type Hint - null type

Page 28: Scalar Types Hints

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

Page 29: Scalar Types Hints

Scalar Type Hint - effet local

Page 30: Scalar Types Hints

Scalar Type Hint - effet local

Affiche :

3

2

Page 31: Scalar Types Hints

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é

Page 32: Scalar Types Hints

Avantages - moins de code, moins de doc (1)

Page 33: Scalar Types Hints

Avantages - moins de code, moins de doc (2)

Page 34: Scalar Types Hints

Avantages - code plus robuste (1-1)

Affiche :

4

4

Page 35: Scalar Types Hints

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

Page 36: Scalar Types Hints

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

Page 37: Scalar Types Hints

Avantages - code plus robuste (2-1)

Page 38: Scalar Types Hints

Avantages - code plus robuste (2-2)

Page 39: Scalar Types Hints

Avantages - code plus robuste (2-2)

Page 40: Scalar Types Hints

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)

Page 41: Scalar Types Hints

Avantages - tests unitaires simplifiés

Page 42: Scalar Types Hints

Avantages - tests unitaires simplifiés

Page 43: Scalar Types Hints

Avantages - tests unitaires simplifiés

Page 44: Scalar Types Hints

Avantages - tests unitaires simplifiés

Page 45: Scalar Types Hints

Avantages - tests unitaires simplifiés

Page 46: Scalar Types Hints

Avantages - tests unitaires simplifiés

Page 47: Scalar Types Hints

Des questions ?