Share
Embed
Tipos de Dados do PHP7 - PHPeste 2019
Slides da apresentação realizada no PHPeste 2019, em Recife.
Códigos de exemplo: https://github.com/devdrops/php7-tipos-de-dados/tree/phpeste-2019
Davi Marcondes Moreira
October 19, 2019
Transcript
55 slides
-
Tipos de Dados do PHP7 Tipos de Dados do PHP7
Photo by on Jordan Heinrichs Unsplash 2019-10-19 @ PHPeste Davi Marcondes Moreira - @devdrops Evolua seu código! -
whoami whoami > Davi Marcondes Moreira > @devdrops > Desenvolvedor
de Software @ Pagar.me > PHP, JavaScript, Kotlin, Go > Terminal é puro ♥ > Defensor do home office e trabalho remoto > Doido por MTB/XCO > Café é a minha religião -
Tipada? Tipada? E o que é uma linguagem E o
que é uma linguagem -
Tipos Suportados Tipos Suportados > boolean > integer > float
> string > array > object > callable > iterable > resource > null > mixed > number > callback > array|object > void -
Tipos Suportados Tipos Suportados > boolean > integer > float
> string > array > object > callable > iterable > resource > null > mixed > number > callback > array|object > void Pseudo tipos -
Sempre a Sempre a primeira primeira declaração após a tag
declaração após a tag -
Aceita somente dois valores: Aceita somente dois valores: 0 0
ou ou 1 1 (desligada por padrão) (desligada por padrão) -
<?php declare(strict_types=1); class Ryu { public function daRaduque(bool $value): bool
{ return !$value; } } function TaPegandoFogoBicho(float $value): float { return $value*100; } 1 2 3 4 5 6 7 8 9 10 11 12 13 -
<?php require __DIR__ . '/exemplo1.php'; $ryu = new Ryu; var_dump($ryu->daRaduque(true));
var_dump($ryu->daRaduque("true")); var_dump(TaPegandoFogoBicho(2.5)); var_dump(TaPegandoFogoBicho(true)); // bool(false) // bool(false) // float(250) // float(100) 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 -
<?php declare(strict_types=1); require __DIR__ . '/exemplo1.php'; $ryu = new Ryu;
var_dump($ryu->daRaduque(true)); var_dump($ryu->daRaduque("true")); var_dump(TaPegandoFogoBicho(2.5)); var_dump(TaPegandoFogoBicho(true)); /* bool(false) PHP Fatal error: Uncaught TypeError: Argument 1 passed to Ryu::daRaduque() must be of the type bool, string given, called in /code/directive/exemplo3.php on line 8 and defined in /code/directive/exemplo1.php:6 Stack trace: #0 /code/directive/exemplo3.php(8): Ryu->daRaduque('true') #1 {main} thrown in /code/directive/exemplo1.php on line 6 */ 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 -
<?php declare(strict_types=1); function OlocoBixo(int $value) { var_dump($value); } $foo =
100; OlocoBixo($foo); // int(100) 1 2 3 4 5 6 7 8 9 10 11 12 -
<?php declare(strict_types=1); function OlocoBixo(int $value) { var_dump($value); } $foo =
"100"; OlocoBixo($foo); // Fatal error: Uncaught TypeError: Argument 1 passed to // OlocoBixo() must be of the type int, string given, called // in /code/exemplo2.php on line 10 and defined in // /code/exemplo2.php:4 // Stack trace: // #0 /code/exemplo2.php(10): OlocoBixo('100') // #1 {main} // thrown in /code/exemplo2.php on line 4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -
<?php declare(strict_types=0); function OlocoBixo(int $value) { var_dump($value); } $foo =
"100"; OlocoBixo($foo); // int(100) 1 2 3 4 5 6 7 8 9 10 11 12 -
<?php declare(strict_types=1); function Eita(): int { return 150; } var_dump(Eita());
// int(150) 1 2 3 4 5 6 7 8 9 10 11 -
<?php declare(strict_types=1); function Eita(): int { return 1.5; } var_dump(Eita());
// Fatal error: Uncaught TypeError: Return value of Eita() must be // of the type int, float returned in /code/exemplo5.php:6 // Stack trace: // #0 /code/exemplo5.php(9): Eita() // #1 {main} // thrown in /code/exemplo5.php on line 6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 -
<?php declare(strict_types=0); function Eita(): int { return 1.5; } var_dump(Eita());
// int(1) 1 2 3 4 5 6 7 8 9 10 11 -
<?php declare(strict_types=1); class Foo {} function Eita(): Foo { return
true; } var_dump(Eita()); // Fatal error: Uncaught TypeError: Return value of Eita() must be // an instance of Foo, bool returned in /code/exemplo7.php:8 // Stack trace: // #0 /code/exemplo7.php(11): Eita() // #1 {main} // thrown in /code/exemplo7.php on line 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 -
<?php declare(strict_types=0); class Foo {} function Eita(): Foo { return
true; } var_dump(Eita()); // Fatal error: Uncaught TypeError: Return value of Eita() must be // an instance of Foo, bool returned in /code/exemplo8.php:8 // Stack trace: // #0 /code/exemplo8.php(11): Eita() // #1 {main} // thrown in /code/exemplo8.php on line 8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 -
<?php declare(strict_types=0); interface Eita { static function preula(): Eita; }
class Batata implements Eita { static function preula(): Batata { // Isso vai dar ruim return new Batata; } } 1 2 3 4 5 6 7 8 9 10 11 12 -
1. Se há um 1. Se há um valor valor
declarado, declarado, null não é uma opção. null não é uma opção. -
2. Se 2. Se null null é permitido, é permitido,
sempre teremos que nos sempre teremos que nos preocupar com null preocupar com null -
<?php declare(strict_types=1); class Foo { /** @var string $name */
private $name; public function __construct(string $name) { $this->name = $name; } public function getName(): string { return $this->name; } public function setName(string $name): void { $this->name = $name; } } 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 -
<?php declare(strict_types=1); class Foo { public string $name; public function
__construct(string $name) { $this->name = $name; } } 1 2 3 4 5 6 7 8 9 10 -
Os tipos são determinados Os tipos são determinados pelo pelo
contexto contexto onde são onde são usadas usadas -
<?php declare(strict_types=1); $foo = 100; // $foo é integer $foo
= false; // $foo é boolean $foo = 1.1; // $foo é float $foo = []; // $foo é array $foo = "aycaramba"; // $foo é string 1 2 3 4 5 6 7 8 -
<?php declare(strict_types=0); $foo = 100; var_dump($foo, gettype($foo)); // int(100) string(7)
"integer" $foo = false; var_dump($foo, gettype($foo)); // bool(false) string(7) "boolean" $foo = 1.1; var_dump($foo, gettype($foo)); // float(1.1) string(6) "double" $foo = []; var_dump($foo, gettype($foo)); // array(0) {} string(5) "array" $foo = "Oi Mãe, Tô No PHPeste :D"; var_dump($foo, gettype($foo)); // string(26) "Oi Mãe, Tô No PHPeste :D" string(6) "string" $foo = "100"; var_dump($foo, gettype($foo)); // string(3) "100" string(6) "string" 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 -
- Tipos ajudam a tornar a intenção do código mais
explícita, além de servir como excelente documentação. - Oferecem garantia de contrato, sem necessidade de filtrar inputs com intval(), is_null() e outras funções. - Não quebra compatibilidade com outras bibliotecas. É 100% compatível com a flexibilidade do PHP. - Depende daquilo que você enxerga como problema. -
Referências Referências - https://en.wikipedia.org/wiki/Strong_and_weak_typing - https://wiki.php.net/rfc/return_types - https://wiki.php.net/rfc/scalar_type_hints_v5 - https://wiki.php.net/rfc/typed_properties_v2
- https://www.php.net/manual/en/language.types.php - https://blog.ircmaxell.com/2015/02/scalar-types-and-php.html - https://blog.pascal-martin.fr/post/in-favor-of-rfc-scalar-type-hints/ - https://pensandonaweb.com.br/o-que-ha-de-novo-no-php-7-4/