TDC 2016 - PHP7

40
Globalcode – Open4education PHP 7 Incompatibilidades e mudanças de comportamento.

Transcript of TDC 2016 - PHP7

Page 1: TDC 2016 - PHP7

Globalcode – Open4education

PHP 7Incompatibilidades e mudanças de

comportamento.

Page 2: TDC 2016 - PHP7

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

Page 3: TDC 2016 - PHP7

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/

Page 4: TDC 2016 - PHP7

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

Page 5: TDC 2016 - PHP7

Globalcode – Open4education

• Mais rápido• Menos memória

PHP7

Page 6: TDC 2016 - PHP7

Globalcode – Open4education

Novidades

https://wiki.php.net/rfc/abstract_syntax_tree

• Abstract Sintax Tree

Page 7: TDC 2016 - PHP7

Globalcode – Open4education

Novidades

https://wiki.php.net/rfc/context_sensitive_lexer

• Contexto léxico sensitivo

Page 8: TDC 2016 - PHP7

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’ ] );

Page 9: TDC 2016 - PHP7

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

Page 10: TDC 2016 - PHP7

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

Page 11: TDC 2016 - PHP7

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

Page 13: TDC 2016 - PHP7

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

Page 14: TDC 2016 - PHP7

Globalcode – Open4education

• Tipos escalares

Novidades

http://php.net/manual/en/language.types.intro.php

Page 15: TDC 2016 - PHP7

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

Page 16: TDC 2016 - PHP7

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

Page 17: TDC 2016 - PHP7

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){ … }});

Page 18: TDC 2016 - PHP7

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”

Page 19: TDC 2016 - PHP7

Globalcode – Open4education

• Unicode• escape de strings

Novidades

// PHP 5.xhtml_entity_decode(‘&#x2603’, 0, ‘UTF-8’);mb_convert_encoding(‘&#x2603’, ‘UTF-8’, ‘HTML-ENTITIES’);json_decode(‘“\\u2603”’);

// PHP 7

echo “\u{2603}”; // ☃

http://php.net/manual/pt_BR/...unicode-codepoint-escape-syntax

Page 20: TDC 2016 - PHP7

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

Page 21: TDC 2016 - PHP7

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

Page 22: TDC 2016 - PHP7

Globalcode – Open4education

Page 23: TDC 2016 - PHP7

Globalcode – Open4education

Incompatibilidades

Page 24: TDC 2016 - PHP7

Globalcode – Open4education

• Variáveis variáveis

Novidades

https://wiki.php.net/rfc/isset_ternary

$foo = “bar”;$bar = “baz”;

echo $$foo; // “baz”

Page 25: TDC 2016 - PHP7

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

Page 26: TDC 2016 - 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

Page 27: TDC 2016 - PHP7

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

Page 28: TDC 2016 - PHP7

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

Page 29: TDC 2016 - PHP7

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

Page 30: TDC 2016 - PHP7

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

Page 31: TDC 2016 - PHP7

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

Page 32: TDC 2016 - PHP7

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

Page 33: TDC 2016 - PHP7

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

Page 34: TDC 2016 - PHP7

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

Page 35: TDC 2016 - PHP7

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

Page 36: TDC 2016 - PHP7

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

Page 37: TDC 2016 - PHP7

Globalcode – Open4education

Importância de testes

Page 38: TDC 2016 - PHP7

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

Page 39: TDC 2016 - PHP7

Globalcode – Open4education

• Email: [email protected]• twitter: @mbaymone • Facebook: facebook.com/mbertholdt• LinkedIn: linkedin.com/in/marceloaymone• Github: github.com/aymone

Contato

Page 40: TDC 2016 - PHP7

Globalcode – Open4education

Obrigado!!!