Globalcode – Open4education
PHP 7Incompatibilidades e mudanças de
comportamento.
Globalcode – Open4education
Sobre mim
• Primeiro contato com php em 2006.• Técnico em processamento de dados, 2007.• Backend PHP e nodejs.• Integração de api’s e soluções para ecommerce.• Chaordic/Linx
Globalcode – Open4education
20 anos do PHP
1995 - Personal Home Page Tools v1.01997 - PHP/fi 21998 - PHP 3 PHP & Zend2000 - PHP 4 Zend engine2004 - PHP 5 Zend Engine II2009 - PHP 5.3 / PHP6 Unicode2012 - PHP 5.42014 - PHP 5.62015 - PHP 7.0 Zend engine III 2016 - PHP 7.01
http://php.net/releases/
Globalcode – Open4education
Suporte
Versão Release Suporte Segurança5.4 03/2012 09/2014 09/20155.5 06/2013 07/2015 07/20165.6 08/2014 12/2016* 12/2018*7.0 12/2015 12/2017 12/2018
http://php.net/supported-versions.php
Globalcode – Open4education
• Mais rápido• Menos memória
PHP7
Globalcode – Open4education
Novidades
https://wiki.php.net/rfc/abstract_syntax_tree
• Abstract Sintax Tree
Globalcode – Open4education
Novidades
https://wiki.php.net/rfc/context_sensitive_lexer
• Contexto léxico sensitivo
Globalcode – Open4education
Novidades
https://wiki.php.net/rfc/context_sensitive_lexer
• Contexto léxico sensitivo
$finder->for( ‘project’ )->where( ‘name’ )->or( ‘code’ )->in( [ ‘4’, ‘5’ ] );
Globalcode – Open4education
• Opcache• Cache secundário persistente em arquivo
Novidades
http://php.net/manual/pt_BR/opcache.configuration.php
zend_extension = opcache.soopcache.enable_cli = 1opcache.file_cache = /tmpopcache.file_cache_only = 1
Globalcode – Open4education
• Novos operadores• Null coalesce
Novidades
https://wiki.php.net/rfc/isset_ternary
$x = null ?? “a” ; // “a”$x = null ?? false ?? 1; // false$x = null ?? “” ?? 1; // “” $x = null ?? 0 ?? 1; // 0
Globalcode – Open4education
• Novos operadores• Spaceship
Novidades
https://wiki.php.net/rfc/combined-comparison-operator#usefulness
[ ] <=> [ ]; // 0 [1, 2] <=> [1,1]; // 1
$a = (object) [“a” => “b”];$b = (object) [“b” => “b”];$a <=> $b // 0
1 <=> 1; // 0 1 <=> 2; // -1 2 <=> 1; // 1
“b” <=> “a”; // 1 “a” <=> “b”; // -1 “a” <=> “aa”; // -1
Globalcode – Open4education
• Throwable Interface• Error
• ArithmeticError• DivisionByZeroError
• AssertionError
• ParseError
• TypeError
• Exception• ErrorException
Novidades
http://php.net/manual/pt_BR/language.errors.php7.php
Globalcode – Open4education
• Funções matemáticas• Intdiv
Novidades
intdiv( 3, 2 ); // 1intdiv( 1, 0 ); // DivisionByZeroErrorintdiv( PHP_INT_MIN, -1 ); // ArithmeticError
http://php.net/manual/pt_BR/function.intdiv.php
Globalcode – Open4education
• Tipos escalares
Novidades
http://php.net/manual/en/language.types.intro.php
Globalcode – Open4education
• Tipos escalares• Modo coercivo x estrito
Novidades
function set(string $input) : int // “1”{
return $input; // 1}$output = set(1.1);
http://php.net/...migration70.new-features.scalar-type-declarations
Globalcode – Open4education
• Tipos escalares• Modo coercivo x estrito
Novidades
declare( strict_types=1 );function set(string $input) : int{
return $input; //throw TypeError }$output = set(1.1); //throw TypeError
https://wiki.php.net/rfc/scalar_type_hints_v5#strict_types_declare_directive
Globalcode – Open4education
• Classes anônimas• extends / implements / traits
Novidades
http://php.net/manual/pt_BR/language.oop5.anonymous.php
use package\ILogger;…$this->setLogger(new class implements ILogger {
public function log($args){ … }});
Globalcode – Open4education
• Constantes do tipo array• define
Novidades
http://php.net/manual/pt_BR/migration70.new-features.php
define (“ANIMALS”, [“dog”, “cat”, “bird”
]);
echo ANIMALS[1]; // “cat”
Globalcode – Open4education
• Unicode• escape de strings
Novidades
// PHP 5.xhtml_entity_decode(‘☃’, 0, ‘UTF-8’);mb_convert_encoding(‘☃’, ‘UTF-8’, ‘HTML-ENTITIES’);json_decode(‘“\\u2603”’);
// PHP 7
echo “\u{2603}”; // ☃
http://php.net/manual/pt_BR/...unicode-codepoint-escape-syntax
Globalcode – Open4education
• Closure::call
Novidades
class A { private $number = 1; } // Sample class$getCb = function( ) { return $this->number; }; // Closure
// PHP 5.6$getNumber = $getCb->bindTo(new A, ‘A’);echo $getNumber( ); // 1
// PHP 7echo $getCb->call(new A); // 1
http://php.net/manual/pt_BR/closure.call.php
Globalcode – Open4education
• Agrupamento de namespaces
Novidades
use some\namespace\ClassA;use some\namespace\ClassB;use some\namespace\ClassC;
use some\namespace\ { ClassA, ClassB, ClassC };use function some\namespace\{ fnA, fnB, fnC };use const some\namespace\ { ConstA, ConstB, ConstC };
http://php.net/manual/pt_BR...group-use-declarations
Globalcode – Open4education
Globalcode – Open4education
Incompatibilidades
Globalcode – Open4education
• Variáveis variáveis
Novidades
https://wiki.php.net/rfc/isset_ternary
$foo = “bar”;$bar = “baz”;
echo $$foo; // “baz”
Globalcode – Open4education
• Variáveis variáveis• Sintaxe uniforme
Novidades
https://wiki.php.net/rfc/isset_ternary
$$foo[ “bar” ][ “baz” ]
$ ( $foo[ “bar” ][ “baz” ] ) // php5
$ ( $foo ) [ “bar” ][ “baz” ] // php7
Globalcode – Open4education
• Errors, handler e try/catch.
Mudanças de comportamento
try { … }catch(Exception $e) { … }catch(Error $er) { … }
function errorHandler( $errno, $errstr) { ... }set_exception_handler(‘errorHandler’);
http://php.net/manual/pt_BR/language.errors.php7.php
Globalcode – Open4education
• Parenteses redundantes.
Mudanças de comportamento
function getArray( ) { return [ 1, 2, 3 ];
};
function squareArray( &$a ) {foreach( $a as &$v ) {
$v **=2;}
}
squareArray( ( getArray( ) ) );
Notice: Only variables should be
passed by reference in /tmp/test.php
on line 13
http://php.net/manual/pt_BR/...variable-handling.parentheses
Globalcode – Open4education
• list• Direção de leitura
Mudanças de comportamento
list( $a[ ], $a[ ], $a[ ] ) = [ 1, 2, 3 ];
// php5var_dump( $a ); // [ 3, 2, 1 ]
// php7var_dump( $a ); // [ 1, 2, 3 ]
http://php.net/manual/pt_BR/...incompatible.variable-handling.list
Globalcode – Open4education
• list• Desempacotar strings • Chamadas vazias
Mudanças de comportamento
$string = “abc”;
// php5list( $a, $b, $c ) = $string; // [ ‘a’, ‘b’, ‘c’ ];
// php7str_split( $string ); // [ ‘a’, ‘b’, ‘c’ ];
http://php.net/manual/pt_BR/...incompatible.variable-handling.list
Globalcode – Open4education
• Iterador foreach
Mudanças de comportamento
$array = [ 0 ];foreach( $array as &$val ) {
var_dump( &$val );$array[ 1 ] = 1;
}// int( 0 )// int( 1 )
http://php.net/manual/pt_BR/...incompatible.foreach
Globalcode – Open4education
• Inteiros• Divisão por zero• Módulo
Mudanças de comportamento
( 1 / 0 ); // Warning: Division by zero %s on line %d
(1 % 0); // PHP Fatal error: Uncaught DivisionByZeroError: Module by zero in %s line %d.
http://php.net/manual/pt_BR/...incompatible.integers.div-by-zero
Globalcode – Open4education
• Palavras reservadas• int, float, bool, string• resource, object, mixed, numeric
Mudanças de comportamento
class string { // ParseError...
}
http://php.net/manual/pt_BR/reserved.other-reserved-words.php
Globalcode – Open4education
• Construtores do php 4
Depreciados
class Example {
// php 4 stylepublic function Example ( ) { ... }
// php 5 stylepublic function __construct ( ) { ... }
}
http://php.net/manual/pt_BR/...deprecated.php4-constructors
Globalcode – Open4education
• Chamadas estáticas a não estáticos de outro contexto
Depreciados
class A { public function test( ) { var_dump( $this ); };
}
class B {public function callNonStaticOfA( ) { A::test( ); }
}
(new B)->callNonStaticOfA( ); // Undefined variable
http://php.net/manual/pt_BR/...deprecated.static-calls
Globalcode – Open4education
• mysql• mysqli ou pdo
• ereg• preg
• call_user_method( )• call_user_func( )
• call_user_method_array()• call_user_func_array( )
Removidos:
http://php.net/manual/pt_BR/migration70.removed-exts-sapis.php
Globalcode – Open4education
• Instalar Virtualbox e Vagrant• Clonar repositório
Testando o php 7
$ git clone https://github.com/rlerdorf/php7dev.git
$ cd php7dev
$ vagrant up
$ vagrant ssh
Globalcode – Open4education
Importância de testes
Globalcode – Open4education
• Slides palestra Rasmus• http://talks.php.net/tokyo15#/
• Guia de migração(Manual PHP)• http://php.net/manual/pt_BR/migration70.php
• Zend infográficos e novidades• http://www.zend.com/en/resources/php7_infographic• http://www.zend.com/en/resources/php7-5-things-to-know-infographic
Referências
Globalcode – Open4education
• Email: [email protected]• twitter: @mbaymone • Facebook: facebook.com/mbertholdt• LinkedIn: linkedin.com/in/marceloaymone• Github: github.com/aymone
Contato
Globalcode – Open4education
Obrigado!!!