aprofunde se no php 5.3
DESCRIPTION
TRANSCRIPT
![Page 1: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/1.jpg)
APROFUNDE-SE NO PHP 5.3
Marcus Vinicius Bastos Leandro http://www.phpnaveia.com.br
![Page 2: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/2.jpg)
Você já está usando o PHP 5.3?
![Page 3: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/3.jpg)
Um breve histórico
PHP 5.0 implementou um novo modelo de objetos
PHP 5.3 nos presenteou com: namespaces, clousures, late static binding, etc...
PHP 6 ?...
![Page 4: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/4.jpg)
Por que migrar para o PHP 5.3?
Mais rápido Melhor gerenciamento de memória Bug fixes Zend Framework 2.0, Symfony 2.0, Doctrine
2.0 vão requerer o PHP 5.3
![Page 5: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/5.jpg)
Definição de Classes
<?php
namespace System\Control;
class UserController {}
$uc = new \System\Control\UserController();
$uc2 = new UserController(); //mesma classe da declaração anterior
![Page 6: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/6.jpg)
Definição de funções
<?php
namespace System\Control;
function hello(){
return “Hello”;
}
echo \System\Control\hello(); //Hello
echo hello(); //Hello
![Page 7: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/7.jpg)
Usando Classes de outro Namespace
<?php
namespace Zend\SignalSlot {
class Signals{}
}
namespace{ //global
use Zend\SignalSlot\Signals;
$signals = new Signals();
}
![Page 8: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/8.jpg)
Alterando o nome
<?php
namespace Squirrel\Core{
class Site{}
}
namespace{
use \Squirrel\Core\Site as SquirrelSite;
$squirrelSite = new SquirrelSite();
}
![Page 9: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/9.jpg)
Chamada Global
<?php
namespace Doctrine{
class Manager{public static function load() { }
}
}
namespace{
\Doctrine\Manager::load();
}
![Page 10: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/10.jpg)
Late Static Binding
![Page 11: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/11.jpg)
O problema
![Page 12: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/12.jpg)
Classe Mãe
<?php
class Foo{
protected static function speak(){return “Hi”;
}
public static function sayHi(){return self::speak();
}
}
![Page 13: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/13.jpg)
Classe Filha
<?php
class Bar extends Foo{
protected static function speak(){return “Hello”;
}
}
![Page 14: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/14.jpg)
“Hi” ou “Hello” ?
<?php
echo Bar::sayHi();
![Page 15: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/15.jpg)
“Hi” ou “Hello” ?
<?php
echo Bar::sayHi(); //Hi
![Page 16: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/16.jpg)
A solução
![Page 17: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/17.jpg)
Classe Mãe
<?php
class Foo{
protected static function speak(){return “Hi”;
}
public static function sayHi(){return static::speak();
}
}
![Page 18: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/18.jpg)
Classe Filha
<?php
class Bar extends Foo{
protected static function speak(){return “Hello”;
}
}
![Page 19: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/19.jpg)
“Hi” ou “Hello”
<?php
echo Bar::sayHi(); //Hello
![Page 20: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/20.jpg)
Clousures / Lambdas
![Page 21: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/21.jpg)
Atribuindo a variáveis
<?php
$sayHi = function(){
return “Hi”;
}
echo $sayHi();
![Page 22: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/22.jpg)
Escopo
<?php
$saudacao = 'Olá';
$falar = function ( $nome ) use ($saudacao){
return $saudacao . ' ' . $nome;
}
echo $falar('Marcus'); //Olá Marcus
![Page 23: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/23.jpg)
Funções anônimas
<?php
function processaAlgo( $valor, $callback ){
…
$callback();
}
processaAlgo( 10, function(){
echo “Processado ...”;
} );
![Page 24: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/24.jpg)
Phar ( PHP Archive )
Distribuindo aplicações em um único arquivo
![Page 25: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/25.jpg)
Criando arquivos Phar
<?php
$phar = new Phar(
'/projeto/app.phar', //caminho para o arquivo
0, //parâmetros
'app.phar' //Alias para referências
);
$phar->startBuffering();
![Page 26: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/26.jpg)
Adicionando arquivos
<?php
$phar->addFile( “/projeto/header.php” );
$phar->addFile( “/projeto/funcoes.php”, “/lib/global.php” );
![Page 27: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/27.jpg)
Adicionando arquivos – outra forma
<?php
$phar['index.php'] = file_get_contents( '/projeto/index.php' );
$phar['header.php'] = '<h1>Header</h1>';
![Page 28: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/28.jpg)
Adicionando arquivos de um diretório
<?php
$phar->buildFromDirectory( '/projeto', '/\.php/' );
![Page 29: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/29.jpg)
Stub
O arquivo Stub é um pedaço de código que será executado quando o arquivo Phar for carregado. Para definir o pedaço de código que será executado ao carregar o arquivo Phar pode-se usar o método setStub que aceita o código em forma de string.
![Page 30: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/30.jpg)
Stub
<?php
$phar->setStub('<?php Phar::mapPhar();
include "phar://app.phar/index.php"; __HALT_COMPILER(); ?>');
![Page 31: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/31.jpg)
Stub
O código deve terminar com a função __HALT_COMPILER().
O método statico Phar::mapPhar() lê e inicializa o arquivo Phar a ser executado.
O streammer phar:// é usado para referenciar os arquivos que estão dento do pacote Phar. No exemplo anterior ao carregar o arquivo app.phar será executado o arquivo index.php.
![Page 32: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/32.jpg)
Stub
Caso não exista nenhum procedimento especial ao carregar o arquivo Phar, e você deseja simplesmente executar um arquivo php diretamente, use o método createDefaultStub. É só passá-lo como parâmetro para o método setStub.
![Page 33: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/33.jpg)
Stub
<?php
$phar->setStub($p->createDefaultStub( 'index.php' ) );
![Page 34: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/34.jpg)
Exemplo completo
index.php
<?php
echo “Olá mundo”;
?>
![Page 35: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/35.jpg)
Script para criar o Phar
criaPhar.php
<?php
$p = new Phar('teste.phar', 0, 'teste.phar');
$p->startBuffering();
$p['index.php'] = file_get_contents("index.php");
// $p->addFile('index.php'); - segunda opção para incluir arquivos.
$p->setStub($p->createDefaultStub('index.php'));
$p->stopBuffering();
![Page 36: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/36.jpg)
Criando o phar
# php criaPhar.php
Após a execução do script acima será criado o arquivo teste.phar
![Page 37: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/37.jpg)
Executando o Phar
# php teste.phar
A saída da execução será:
Olá Mundo
![Page 38: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/38.jpg)
E tem mais...
Funções de internacionalização SQLite versão 3 FileInfo: mime-type, encoding -
http://www.php.net/manual/en/ref.fileinfo.php
![Page 39: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/39.jpg)
DateTime Object
Adicionar o subtrair datas Calcular a diferença entre duas datas Definir/recuperar unix timestamp http://www.brandonsavage.net/cool-datetime-
functions-in-php-5-3/
![Page 40: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/40.jpg)
Novas estruturas SPL
![Page 41: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/41.jpg)
SPL Stack
Push e Pop Last in, First Out (LIFO) Usa menos memória do que arrays para
grandes pilhas ( mais que 5.000 elementos )
![Page 42: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/42.jpg)
SPLQueue
Enqueue & Dequeue First in, First Out (FIFO) Mais rápida e usa menos memória que arrays
![Page 43: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/43.jpg)
SPLHeap
Insert & Remove Reordena elementos baseado em
comparações Mais rápido e usa menos memória que arrays
![Page 44: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/44.jpg)
Veja mais em
http://matthewturland.com/2010/05/20/new-spl-features-in-php-5-3/
![Page 45: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/45.jpg)
GOTO
Essa parte pula
![Page 46: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/46.jpg)
Nova sintaxe
![Page 47: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/47.jpg)
__invoke()
<?php
class Foo{
public function __invoke($x){return $x + $x;
}
}
$foo = new Foo();
echo $foo(2); //4
![Page 48: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/48.jpg)
__callStatic()
<?php
class Foo{
public static function __callStatic( $name, $args ){return $name . ' foi chamada estaticamente';
}
}
echo Foo::bar(); //bar foi chamada //estaticamente
![Page 49: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/49.jpg)
__DIR__
<?php
echo dirname(__FILE__);
echo __DIR__;
![Page 50: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/50.jpg)
Novo ternário
<?php
$user->setApelido('mvleandro');
$apelido = $user->getApelido()?:'Não informado';
echo $apelido; //mvleandro
![Page 51: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/51.jpg)
Referência
PHP Manual – http://php.net/ Bradley Holt – http://bradley-holt.com/ PHP Brasil - http://phpbrasil.com/
![Page 52: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/52.jpg)
Perguntas?
![Page 53: Aprofunde se no php 5.3](https://reader034.vdocuments.site/reader034/viewer/2022051312/546be2d0b4af9f932c8b4dbc/html5/thumbnails/53.jpg)
Obrigado
Marcus Vinicius Bastos Leandro
http://www.phpnaveia.com.br
Twitter: @mvleandroE-mail: [email protected]: [email protected]: (21)-8211-9096