PHP Anti Patterns
-
Upload
theoziran-lima -
Category
Technology
-
view
1.619 -
download
3
Transcript of PHP Anti Patterns
![Page 1: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/1.jpg)
PHP Anti-patternsO que NÃO fazer com PHP
![Page 2: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/2.jpg)
Sobre mim
• Theoziran Lima
– Análise e Desenvolvimento de Sistemas (Faculdade iDEZ)
– Analista de sistemas (Acade)
– Desenvolvedor PHP (Shoprizer)
– Colunista iMasters
... e eu não sou o carinha da capa...
![Page 3: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/3.jpg)
O que são anti-
patterns?
![Page 4: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/4.jpg)
Significa fora do padrão, ou simplesmente ter maus hábitos, no
nosso caso de codificação.
![Page 5: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/5.jpg)
Porque devemos saber o que não fazer (anti-patterns)?
![Page 6: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/6.jpg)
Geralmente iniciantes tendem a criar códigos utilizando “anti-
patterns”
![Page 7: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/7.jpg)
Porque vamos conhecer os problemas/erros que foram
enfrentados por desenvolvedores mais experientes, assim não cometeremos
os mesmos erros.
![Page 8: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/8.jpg)
Então qual padrão de projeto devo usar?
![Page 9: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/9.jpg)
Não existe isso, temos os padrões para resolver problemas específicos sendo tratados de forma
genérica.
• Padrões de criação
– Factory
– Singleton
• Padrões estruturais
– Adapter
– Bridge
... vamos voltar aos anti-patterns
![Page 10: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/10.jpg)
... outro “problema” ...
![Page 11: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/11.jpg)
PHP é fácil, também é mais fácil cometer erros
• Quanto mais fácil a linguagem, mais fácil fazer gambiarras;
• Por ser fácil, muitas pessoas desenvolvem sem ter nenhuma base (conceitos) de desenvolvimento de software;
• O desenvolvedor se sente tentado a resolver o problema de forma mais rápida.
![Page 12: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/12.jpg)
PHP é fácil, também é mais fácil cometer erros
• Invasão de camadas, quando se está utilizando um MVC, ou pelo menos tentando.
![Page 13: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/13.jpg)
“Nenhuma linguagem, framework, impede do desenvolvedor fazer gambiarra” Felipe Ribeiro, “PHP não é coisa de moleque”
Esqueceram que somos os mestres das soluções?
![Page 14: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/14.jpg)
Spaghetti é o padrão mais
utilizado
![Page 15: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/15.jpg)
No anti-pattern spaghetti, o “V” de MVC faz tudo.
![Page 16: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/16.jpg)
É importante ter estilo ...
![Page 17: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/17.jpg)
Estilos de codificação
• Seu código não deve ser percebido dentro do sistema;
• Sempre desenvolva com E_STRICT e E_NOTICE ativos;
• Filtre suas entradas e escape suas saídas;
• Quando possível utilize type-casts em suas funções ou métodos.
![Page 18: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/18.jpg)
Use com cuidado
• $_REQUEST
• __get, __set, __isset, __unset
• __call, __callStatic
• __autoload
• @
• <?=
![Page 19: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/19.jpg)
“Evite” fazer gambiarras ...“Evite” fazer gambiarras ...
![Page 20: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/20.jpg)
... uma dia ela vai
explodir ...
![Page 21: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/21.jpg)
Não conte com a sorte para seu sistema funcionar
Não conte com a sorte para seu sistema funcionar
![Page 22: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/22.jpg)
Framework, você tá brincando?Framework, você tá brincando?
![Page 23: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/23.jpg)
DIY – Do it yourself
• Desperdício de tempo
– Desenvolvimento
– Testes
– Documentação
– Manutenção
• Criar bus desnecessários
![Page 24: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/24.jpg)
Overdesign
• Limite-se a fazer o que o cliente deseja, ou ao que foi requisitado.
• Não fique refazendo o sistema do zero todas vez que cometer um erro.
![Page 25: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/25.jpg)
Overdesign
• Quanto mais complexo for sua arquitetura:
– O código será mais complexo;
– Mais bugs você vai ter;
– Mais tempo de desenvolvimento irá custar;
– A probalidade o projeto falhar é alta.
![Page 26: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/26.jpg)
PHP não é Java, C++, Python, Ruby on Rails
<?php
class String{
private $str;
public function __construct($str){
$this->str = $str;
}
public function __toString(){
return $this->str;
}
}
![Page 27: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/27.jpg)
... KISS, ainda funciona ...
![Page 28: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/28.jpg)
![Page 29: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/29.jpg)
Pense em segurança em todas as fases do projeto
Influenciará no design e no desenvolvimento
![Page 30: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/30.jpg)
Dica
Aproveitem pra estudar os patterns e os anti-patterns que vai nos poupar tempo de
passar experiências desagradáveis.
![Page 31: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/31.jpg)
Alguma dúvida ?
![Page 32: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/32.jpg)
Referências
• PHP Quebec Conference
– http://conf.phpquebec.com/
• Wikipédia
– Anti-patterns
– Design Pattern
![Page 33: PHP Anti Patterns](https://reader036.fdocument.pub/reader036/viewer/2022062319/555b2fe8d8b42afc348b4b29/html5/thumbnails/33.jpg)
Sigam-me os bons...
@theoziranwww.theoziran.com.br