Share
Embed
Top 10 Features do PHP Moderno
Nesta palestra Galvão apresenta as 10 features mais importantes (na sua opinião) do PHP moderno.
Er Galvão Abbott
May 19, 2017
Transcript
40 slides
-
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 1 / 40 Top 10 Features do PHP Moderno www.galvao.eti.br TOP 10 Features do php Moderno -
Presidente da ABRAPHP – Associação Brasileira de Profissionais PHP Diretor
da PHP Conference Brasil Contribui para a tradução da documentação oficial Atua como Zend Framework Evangelist para o ZTeam, da Zend. Professor Convidado de Pós-Graduação, PR e SC. 22+ anos desenvolvendo sistemas e aplicações com interface web 17+ destes com PHP 9+ com Zend Framework Palestrante em eventos nacionais e internacionais Instrutor de cursos presenciais e a distância Fundador* e membro do GU PHPRS Site: https://www.galvao.eti.br/ http://people.php.net/galvao Twitter: @galvao Facebook: https://www.facebook.com/galvao.eti.br/ Slides e Documentos: http://slideshare.net/ergalvao https://speakerdeck.com/galvao Github: http://github.com/galvao Posts: https://medium.com/@galvao Quem?! CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott - 5/31/17 - 2 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno -
Objetivo CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 5/31/17 - 3 / 40 www.galvao.eti.br → Atualizar quem já trabalha com a linguagem; → Eliminar um pouco do estigma que a linguagem possui; → Demonstrar o poder que a linguagem possui atualmente; Top 10 Features do PHP Moderno -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 4 / 40 www.galvao.eti.br 1. Namespaces PHP 5.3 ≃ 8 anos Top 10 Features do PHP Moderno -
Namespaces CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 5/31/17 - 5 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php namespace MyProject; class User { ... <?php use MyProject\User; -
Namespaces CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 5/31/17 - 6 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno código função objeto namespace -
Namespaces CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 5/31/17 - 7 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno namespaces autoloader composer packagist { "require": { "guzzlehttp/guzzle": "^6.2", "phpmailer/phpmailer": "^5.2" } } -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 8 / 40 www.galvao.eti.br 2. STH / RTD / Strict Types PHP 7.0 / 7.1 / ... ≃ 1.5 ano Top 10 Features do PHP Moderno -
STH / RTD / Strict Types CC Attribution-ShareAlike 3.0 Unported
License by Er Galvão Abbott - 5/31/17 - 9 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php declare(strict_types = 1); function soma(int $x, float $y): float { return $x + $y; } $x = soma(1, 2.5); $x = soma(1.2, 2.7); $x = soma(‘1’, 12); $x = soma(2, 8); x x -
void* > Strict Types CC Attribution-ShareAlike 3.0 Unported License by
Er Galvão Abbott - 5/31/17 - 10 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php function soma(int $x, float $y): void { return $x + $y; } x <?php function soma(int &$x, int $y): void { $x = $x + $y; } $x = 0; soma($x, 2); * PHP 7.1 -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 11 / 40 www.galvao.eti.br 3. Funções Anônimas Top 10 Features do PHP Moderno PHP 5.3 ≃ 8 anos -
Funções Anônimas CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 5/31/17 - 12 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php function soma(Closure $x, int $y): float { return $x() + $y; } <?php echo soma(function ():int { return 2; }, 8); -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 13 / 40 www.galvao.eti.br 4. Hash de senhas Top 10 Features do PHP Moderno PHP 5.5 ≃ 4 anos -
password_hash CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 5/31/17 - 14 / 40 www.galvao.eti.br Os 4 Pilares de Segurança de uma Aplicação $senha = password_hash($plain, PASSWORD_ DEFAULT BCRYPT [, $options]); hash_resultante dado original algo salt opções -
password_hash CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott
- 5/31/17 - 15 / 40 www.galvao.eti.br Os 4 Pilares de Segurança de uma Aplicação password_hash password_verify password_needs_rehash -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 16 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php $crypto = [ 'algo' => PASSWORD_DEFAULT, 'options' => [ 'cost' => 10, ], ]; $inputPass = 'mypass'; $pass = password_hash($inputPass, $crypto['algo'], $crypto['options']); if (password_verify($inputPass, $pass)) { $crypto['options']['cost'] = 12; if (password_needs_rehash($inputPass, $crypto['algo'], $crypto['options'])) { $newPass = password_hash($inputPass, $crypto['algo'], $crypto['options']); } } password_hash -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 17 / 40 www.galvao.eti.br 5. Filtragem e Validação Top 10 Features do PHP Moderno PHP 5.2 ≃ 11 anos -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 18 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno Filtragem e Validação filter_var($dado, FILTER_ SANITIZE_ VALIDATE_ EMAIL ENCODED MAGIC_QUOTES NUMBER_FLOAT NUMBER_INT SPECIAL_CHARS FULL_SPECIAL_CHARS STRING STRIPPED URL UNSAFE_RAW BOOLEAN EMAIL FLOAT INT IP MAC REGEXP URL ); CALLBACK -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 19 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php $telefone = filter_var($_POST['telefone'], FILTER_CALLBACK, [ 'options' => function ($dado) { return preg_replace('/[^\d|\-|\.]/', '', $dado); }] ); if (!filter_var($telefone, FILTER_VALIDATE_REGEXP, [ 'options' => [ 'regexp' => '/[\d|\-|\.]/' ] ])) { die('Telefone inválido'); } Filtragem e Validação -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 20 / 40 www.galvao.eti.br 6. Criptografia Top 10 Features do PHP Moderno PHP 5 ≃ 12 anos -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 21 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno Criptografia openssl_ encrypt decrypt ($dado, method, key, options, IV); cifra + modo -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 22 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php define('CRYPTO', [ 'CIPHER' => 'aes-256-ctr', 'KEY' => '46f9cae861ec7c5474102c0a901b7a44', 'IV' => '35e59367dc417e703757341520d1486d', ]); $crypt = openssl_encrypt($data, CRYPTO['CIPHER'], CRYPTO['KEY'], 0, CRYPTO['IV'] ); $decrypt = openssl_decrypt($crypt, CRYPTO['CIPHER'], CRYPTO['KEY'], 0, CRYPTO['IV'] ); Criptografia -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 23 / 40 www.galvao.eti.br 7. CSPRNG Top 10 Features do PHP Moderno PHP 4/7* ≃17 anos / ≃ 1.5 ano -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 24 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php $randomInt = random_int(0, 2); $randomStr = base64_encode(random_bytes(10)); CSPRNG <?php // PHP >= 4 $randomInt = mt_rand(0, 2); // $randomStr = Alguma implementação customizada -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 25 / 40 www.galvao.eti.br 8. Traits Top 10 Features do PHP Moderno PHP 5.4 ≃ 5 anos -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 26 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php namespace MyProject\Trait; trait Singleton { private static $instance; public static function getInstance() { $class = __CLASS__; self::$instance = self::$instance ?? new $class; return self::$instance; } } Traits -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 27 / 40 www.galvao.eti.br 9. Generators Top 10 Features do PHP Moderno PHP 5.5 ≃ 4 anos -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 28 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php $begin = new DateTime(); $array = []; foreach (range(0, 1000000) as $int) { $array[] = $int; } $end = new DateTime(); $interval = $begin->diff($end); echo 'O array $array contém ' . number_format(count($array)) . “ elementos.\n”; echo $interval->format('%S segundos.') . “\n”; Generators O array $array contém 1,000,001 elementos. 00 segundos. -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 29 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php $begin = new DateTime(); $array = []; foreach (range(0, 10000000) as $int) { $array[] = $int; } $end = new DateTime(); $interval = $begin->diff($end); echo 'O array $array contém ' . number_format(count($array)) . “ elementos.\n”; echo $interval->format('%S segundos.') . “\n”; Generators Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 536870920 bytes) in /var/www/.../g1.php on line 6 536 Mb – Memória excedida em 402 Mb -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 30 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php function xrange($start, $limit) { for ($i = $start; $i <= $limit; $i++) { yield $i; } } $begin = new DateTime(); foreach (xrange(0, 10000000) as $int) { $array[] = $int; } $end = new DateTime(); $interval = $begin->diff($end); echo 'O array $array contém ' . number_format(count($array)) . “ elementos.\n”; echo $interval->format('%S segundos.') . “\n”; Generators Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 134217736 bytes) in /var/www/.../g1.php on line 6 134 Mb : Memória excedida em 8 bytes -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 31 / 40 www.galvao.eti.br 10. Abstração de BD Top 10 Features do PHP Moderno PHP 5* ≃ 12 anos -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 32 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php define('DB', [ 'DRIVER' => 'mysql', 'HOST' => 'localhost', 'PORT' => 3306, 'NAME' => 'teste', 'USER' => 'aluno', 'PASS' => 'aluno', ]); $dsn = DB['DRIVER'] . ':host=' . DB['HOST'] . ';port=' . DB['PORT'] . ';dbname=' . DB['NAME']; $dbh = new PDO($dsn, DB['USER'], DB['PASS']); $sql = "INSERT into foo (bar) VALUES ('baz')"; $dbh->exec($sql); $dbh = NULL; PDO -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 33 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno <?php define('DB', [ 'DRIVER' => 'pgsql', 'HOST' => 'localhost', 'PORT' => 5432, 'NAME' => 'teste', 'USER' => 'aluno', 'PASS' => 'aluno', ]); $dsn = DB['DRIVER'] . ':host=' . DB['HOST'] . ';port=' . DB['PORT'] . ';dbname=' . DB['NAME'] . ‘;user=’ . DB[‘USER’] . ‘;password=’ . DB[‘PASS’]; $dbh = new PDO($dsn); $sql = "INSERT into foo (bar) VALUES ('baz')"; $dbh->exec($sql); $dbh = NULL; PDO -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 34 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno MS SQL Server Sybase Firebird IBM DB2 IBM Informix Oracle ODBC SQLite ... PDO++ -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 35 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno Concluindo... -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 36 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno Concluindo… R -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 37 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno Concluindo… RT -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 38 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno Concluindo… RTF -
CC Attribution-ShareAlike 3.0 Unported License by Er Galvão Abbott -
5/31/17 - 39 / 40 www.galvao.eti.br Top 10 Features do PHP Moderno Concluindo… RTFM!!! Concluindo… RTFM!!! http://php.net/manual/en/index.php Concluindo… RTFM!!! Concluindo… RTFM!!! -
Muito obrigado! CC Attribution-ShareAlike 3.0 Unported License by Er Galvão
Abbott - 5/31/17 - 40 / 40 www.galvao.eti.br ? Dúvidas? ↓ Críticas? ↑ Elogios?! Top 10 Features do PHP Moderno