PHP freakshow 2 PHP Experience 2016

21
PHP FREAKSHOW LEONARDO TUMADJIAN PHP EXPERIENCE 2016

Transcript of PHP freakshow 2 PHP Experience 2016

Page 2: PHP freakshow 2 PHP Experience 2016

ANTES DE COMEÇARMOS:Contribua com sua comunidade mais próxima, se tiver um tempo ;)

Como contribuir?

Page 3: PHP freakshow 2 PHP Experience 2016

ATENÇÃO!O conteúdo a seguir pode conter cenas fortes, códigos sujos, violência contra as boas práticas, esquisitices, loucuras e muito mais..

Em caso de fortes problemas cardíacos prefira: Design Patterns, SOLID, DDD e etc..

Não tente fazer isso em casa, e muito menos no trabalho, obrigado!

Page 4: PHP freakshow 2 PHP Experience 2016

WELLCOME!Sejam bem vindos ao PHP Freak Show, o show já vai começar, preparem-se.

Page 5: PHP freakshow 2 PHP Experience 2016

VARIÁVEIS VARIÁVEIS!$freak = 'ola';$ola = 'como'; $como = 'vai'; $vai = 'voce'; $voce = 'cara';$cara = 'zoeira'; $zoeira = 'never ends!';

echo $$$$$$$freak; // Imprime: never ends!?

// ... class Robot private $precious1 = 'Nooo 1'; protected $precious2 = 'Nooo 2';

public function __get($prop) return $this­>$prop; // <­ Explodindo o encapsulamento! // ... $robot = new Robot(); echo $robot­>precious1; // Nooo 1 echo $robot­>precious2; // Nooo 2

Page 6: PHP freakshow 2 PHP Experience 2016

AGORA A ABERRAÇÃO:Ainda mais...

// ... $obj = 'myRobot'; $class = 'Robot'; $param1 = 'Robotron'; $param2 = 5532; $method = 'say'; $word = 'Hello Humans';

$$obj = (new $class($param1, $param2))­>$method($word); // Imprime Hello Humans // ...

Page 7: PHP freakshow 2 PHP Experience 2016

STANDARD PHP LIBRARY

USAREMOS:1. ArrayIterator2. ArrayAccess(Interface)

Page 8: PHP freakshow 2 PHP Experience 2016

PRIMEIRA LOUCURAclass Writer extends \ArrayIterator public function offsetGet($index) echo $index . ' '; return $this;

$whiter = new Writer(); $whiter['Hello']['World']; // Imprime: Hello World

$whiter['But']['when']['this']['is']['over?']; // Imprime: But when this is over?

Page 9: PHP freakshow 2 PHP Experience 2016

SEMPRE PODE PIORAR!

Page 10: PHP freakshow 2 PHP Experience 2016

POSSO?class Crazy implements \ArrayAccess // ... some code here public function offsetGet($offset) $this­>phrase[] = $this­>tagIn() . $offset . $this­>tagOut(); return $this; // ... public function color($colorHex) $last = &$this­>phrase[count($this­>phrase)­1]; $last = '<div style="color: ' . $colorHex .'">' . $last . '</div>'; return $this; // ... more code bellow

Page 11: PHP freakshow 2 PHP Experience 2016

AGORA VAMOS USA-LA?

E agora? Onde está seu deus?// Using the class Crazy! Roda no PHP 7! (new Crazy) ['só da']­>color('brown') ['loco'] ['aqui']­>color('red') ['olha o que'] ['esse barba'] ['esta']­>color('blue') ['fazendo'] ['que']­>color('red') ['maluco'] ['das']­>color('green') ['ideias'] ­>write();

Page 12: PHP freakshow 2 PHP Experience 2016

VARIADICSPHP 5.6+

$class = 'Robot'; $method = 'talk'; $arr = ['hello', 'world'];

// Old but GOLD call_user_func_array([new $class, $method], $arr);

// OR WARNING PHP 5.6+ $robot = (new $class)­>$method(...$arr);

Page 13: PHP freakshow 2 PHP Experience 2016

MÉTODOS MÁGICOSO mago

Aos Javeiros!

Page 14: PHP freakshow 2 PHP Experience 2016

Usando o método __callclass DataMaker protected $matrix; // ... public function __call($name, $args) $this­>matrix[$name] = $args[0]; return $this; // ...

$dados = (new DataMaker) ­>nome('Leonardo') ­>telefone('11 95555­2233') ­>endereco('Rua Teste 123') ­>teste('colocando char especial') ­>toClass('Customer'); var_dump($dados);

Page 15: PHP freakshow 2 PHP Experience 2016

RETORNO:object(Customer)[2] public 'nome' => string 'Leonardo' (length=8) public 'telefone' => string '11 97379­7752' (length=13) public 'endereco' => string 'Rua Teste 123' (length=13) public 'teste' => string 'colocando char especial' (length=23)

Page 16: PHP freakshow 2 PHP Experience 2016

ALGUMAS IDEIAS USANDO UM ROUTER:// A crazy Router interface (new GET)['/ola'] ­>controller('Carro') ­>action('hello'); // A Fluid Interface Programming request('POST') ­>to('/cliente') ­>controller('Carro') ­>action('hello');

// Pratical Router $post = new Post;

$post['/hello'] = function ($name) return 'Hello ' . $name; ;

Page 17: PHP freakshow 2 PHP Experience 2016

BOM ACHO QUE ACABAMOS..// freak instanciator function __($class, array $parms=[]) return new $class(...$parms);__('FreakPower') [function () echo '1'; ][function () echo '2'; ][function () echo '3'; ][function () echo '4'; ][function () echo '5'; ]­>fire(); // Dispara as closures aleatoriamente, Imprime, ex: 43152

Hey! espera! não, não faça isso!

Page 18: PHP freakshow 2 PHP Experience 2016

UTILIDADE PÚBLICA..Owesome solution by Ocramius

$reader = function & ($object, $property) $value = & Closure::bind(function & () use ($property) return $this­>$property; , $object, $object)­>__invoke();

return $value;;

$kitchen = new Kitchen(); $cake = & $reader($kitchen, 'cake'); // $this­>cake is private $cake = 'sorry, I ate it!';

var_dump($kitchen);

Reference: Accessing private PHP class members without reflection

Page 19: PHP freakshow 2 PHP Experience 2016

ESPERO QUE TENHAM GOSTADO“Yolo lo!” ―Mago do Age of Empires

use function yolo\y;

yolo\yolisp(y('yolo\yolo', y('lambda', y('request'), y('new', YoLo\resPONsE::clASS, y('quote', "yolo \u1f640")) )));

https://yolophp.computer/https://github.com/igorw/yolo

Page 20: PHP freakshow 2 PHP Experience 2016

OBRIGADO!

Page 21: PHP freakshow 2 PHP Experience 2016

HTTPS://JOIND.IN/TALK/328B5SLIDES E EXEMPLOS EM:

HTTPS://GITHUB.COM/LEOQBC/PHP-FREAKSHOW2