Orientação a Objetos em PHP




                                              augusto@phpsp.org.br

                      ...
AUGUSTO PASCUTTI


   •4    anos de experiência

   • ZCE

   • Fundador/coordenador      PHPSP

   • Habari, CakePHP




...
OBJETIVOS


Thursday, January 28, 2010
ORIENTAR


Thursday, January 28, 2010
PHP 5


Thursday, January 28, 2010
USAR


Thursday, January 28, 2010
ORIENTANDO-SE




Thursday, January 28, 2010
PROGRAMA PROCEDURAL ?




Thursday, January 28, 2010
ESQUEÇA !




Thursday, January 28, 2010
PENSE DIFERENTE




Thursday, January 28, 2010
PENSE OO




Thursday, January 28, 2010
CLASSES



Thursday, January 28, 2010
BASE DA OO



Thursday, January 28, 2010
MOLDES



Thursday, January 28, 2010
INSTRUÇÕES



Thursday, January 28, 2010
SEM VONTADE PRÓPRIA



Thursday, January 28, 2010
CLASSES



Thursday, January 28, 2010
CLASSES



Thursday, January 28, 2010
CLASSES



Thursday, January 28, 2010
CLASSES



Thursday, January 28, 2010
• Uma classe por arquivo

                  • Padrões de nomenclatura

                         • Padrão_Zend_ou_Pear
    ...
OBJETOS
Thursday, January 28, 2010
FILHOS DAS CLASSES




Thursday, January 28, 2010
INFORMAÇÕES




Thursday, January 28, 2010
INDEPENDENTES




Thursday, January 28, 2010
EXECUTORES




Thursday, January 28, 2010
EXECUTAM




Thursday, January 28, 2010
ATRIBUTOS
Thursday, January 28, 2010
DECLARADOS NA CLASSE
Thursday, January 28, 2010
USADOS NOS OBJETOS
Thursday, January 28, 2010
PROPRIEDADES
Thursday, January 28, 2010
INFORMAÇÕES
Thursday, January 28, 2010
VARIÁVEIS
Thursday, January 28, 2010
4
Thursday, January 28, 2010
VARIÁVEIS
Thursday, January 28, 2010
5
Thursday, January 28, 2010
VARIÁVEIS
Thursday, January 28, 2010
MÉTODOS
Thursday, January 28, 2010
DECLARADOS NA CLASSE
Thursday, January 28, 2010
USADOS PELOS OBJETOS
Thursday, January 28, 2010
FUNÇÕES
Thursday, January 28, 2010
FAZEM COISAS
Thursday, January 28, 2010
DÃO E RECEBEM
Thursday, January 28, 2010
4
Thursday, January 28, 2010
DÃO E RECEBEM
Thursday, January 28, 2010
5
Thursday, January 28, 2010
DÃO E RECEBEM
Thursday, January 28, 2010
AUTOLOAD




Thursday, January 28, 2010
AUTOLOAD
           •Executado na instanciação
           •Executado somente para classes não declaradas
           •Nativ...
AUTOLOAD




Thursday, January 28, 2010
AUTOLOAD




Thursday, January 28, 2010
MEMBROS DA CLASSE
Thursday, January 28, 2010
CONSTANTES




Thursday, January 28, 2010
CONSTANTES




Thursday, January 28, 2010
ESTÁTICOS



Thursday, January 28, 2010
ESTÁTICOS



Thursday, January 28, 2010
ESTÁTICOS



Thursday, January 28, 2010
MENSAGENS
Thursday, January 28, 2010
PODER DA OO
Thursday, January 28, 2010
TROCA DE INFORMAÇÕES
Thursday, January 28, 2010
TROCA DE INFORMAÇÕES
Thursday, January 28, 2010
HERANÇA
Thursday, January 28, 2010
CLASSE PAI
Thursday, January 28, 2010
CLASSE FILHA
Thursday, January 28, 2010
VISIBILIDADE




Thursday, January 28, 2010
PUBLIC




Thursday, January 28, 2010
PROTECTED




Thursday, January 28, 2010
PRIVATE




Thursday, January 28, 2010
POLIMORFISMO
Thursday, January 28, 2010
ERA UMA COISA
Thursday, January 28, 2010
AGORA É OUTRA
Thursday, January 28, 2010
SOBRECARGA
Thursday, January 28, 2010
NA VERDADE: SOBRESCRITA
Thursday, January 28, 2010
NA VERDADE: SOBRESCRITA
Thursday, January 28, 2010
FINAL
Thursday, January 28, 2010
IMPEDE O POLIMORFISMO
Thursday, January 28, 2010
USADO EM CLASSES ...
Thursday, January 28, 2010
E MÉTODOS
Thursday, January 28, 2010
E MÉTODOS
Thursday, January 28, 2010
ABSTRATO
Thursday, January 28, 2010
NÃO PODE SER INSTANCIADA
Thursday, January 28, 2010
USADA COMO PAI
Thursday, January 28, 2010
USADA COMO PAI
Thursday, January 28, 2010
USADA COMO PAI
Thursday, January 28, 2010
MÁGICA
Thursday, January 28, 2010
MÉTODOS MÁGICOS
Thursday, January 28, 2010
EXECUTADOS PELO PHP
Thursday, January 28, 2010
14 EXISTENTES NO PHP 5.3
Thursday, January 28, 2010
SEMPRE COMEÇAM COM “_ _”
Thursday, January 28, 2010
CONSTRUINDO




Thursday, January 28, 2010
__CONSTRUCT


           •Executado na instanciação do objeto
           •Não pode retornar nada
           •Pode sofrer s...
__CONSTRUCT




Thursday, January 28, 2010
DESTRUINDO
Thursday, January 28, 2010
•Executado quando objeto sai da memória
            •Final do script
            •unset() do objeto
           •Pode ser ú...
__DESTRUCT
Thursday, January 28, 2010
MOSTRANDO




Thursday, January 28, 2010
__TOSTRING
       •Chamado quando objeto é usado como String
       •O método DEVE retornar uma String
       •Útil para o...
__TOSTRING




Thursday, January 28, 2010
OUTROS MÉTODOS MÁGICOS

            •__call
            •__get
            •__set
            •__get
            •__isset
...
INTERFACES
Thursday, January 28, 2010
“CLASSES” GENÉRICAS
Thursday, January 28, 2010
DEFINEM COMPORTAMENTOS
Thursday, January 28, 2010
TIPOS DE DADOS
Thursday, January 28, 2010
TIPOS DE DADOS
Thursday, January 28, 2010
•Mais genéricos que Classes Abstratas

            •Uma classe pode implementar várias Interfaces


                      ...
EXCEPTIONS




Thursday, January 28, 2010
TRABALHANDO
Thursday, January 28, 2010
OBJETOS E REFERÊNCIAS
Thursday, January 28, 2010
OBJETOS E REFERÊNCIAS
Thursday, January 28, 2010
OBJETOS E REFERÊNCIAS
Thursday, January 28, 2010
OBJETOS E REFERÊNCIAS
Thursday, January 28, 2010
TYPE HINT
Thursday, January 28, 2010
•Forçam um tipo a um argumento
            •Só podem ser usados como tipo:
             •Array
             •Classes




 ...
TYPE HINT
Thursday, January 28, 2010
TYPE HINT
Thursday, January 28, 2010
INSTANCE OF
Thursday, January 28, 2010
•Checa se um objeto é uma determinada instância
                •Retorna boolean
                •Um objeto pode ser checa...
INSTANCE OF
Thursday, January 28, 2010
BANCO DE DADOS
Thursday, January 28, 2010
ABSTRAIA
Thursday, January 28, 2010
•Melhora segurança
                     •Centraliza execuções
                     •Melhor manutenção
                    ...
NÃO REINVENTE A RODA
Thursday, January 28, 2010
USE A PDO
Thursday, January 28, 2010
•O que ela é:
              •Abstração de Acesso para banco de dados
              •Nativo do PHP 5.1
              •Apren...
•O que ela NÃO é:
                      •Não abstrai suas queries
                      •Não evita instalação de drivers d...
USANDO A PDO
Thursday, January 28, 2010
USANDO A PDO
Thursday, January 28, 2010
•Agilizam a execução de queries repetidas
                     •Implementam um pouco de segurança
                     •Si...
PDO STATEMENTS
Thursday, January 28, 2010
PARÂMETROS
Thursday, January 28, 2010
RETORNO ID INSERIDO
Thursday, January 28, 2010
DESIGN PATTERNS
Thursday, January 28, 2010
•Solução comum para problemas comuns
                     •Independem de linguagem
                     •Vocuabulário comu...
•Singleton
                     •Factory
                     •Strategy
                     •Data Access Object
         ...
FRAMEWORKS
Thursday, January 28, 2010
Augusto Pascutti
                                               augusto@phpsp.org.br
                                     ...
Upcoming SlideShare
Loading in …5
×

Orientação a Objetos na prática em php

5,418 views

Published on

Published in: Technology
  • Be the first to comment

Orientação a Objetos na prática em php

  1. 1. Orientação a Objetos em PHP augusto@phpsp.org.br @augustohp Thursday, January 28, 2010
  2. 2. AUGUSTO PASCUTTI •4 anos de experiência • ZCE • Fundador/coordenador PHPSP • Habari, CakePHP Thursday, January 28, 2010
  3. 3. OBJETIVOS Thursday, January 28, 2010
  4. 4. ORIENTAR Thursday, January 28, 2010
  5. 5. PHP 5 Thursday, January 28, 2010
  6. 6. USAR Thursday, January 28, 2010
  7. 7. ORIENTANDO-SE Thursday, January 28, 2010
  8. 8. PROGRAMA PROCEDURAL ? Thursday, January 28, 2010
  9. 9. ESQUEÇA ! Thursday, January 28, 2010
  10. 10. PENSE DIFERENTE Thursday, January 28, 2010
  11. 11. PENSE OO Thursday, January 28, 2010
  12. 12. CLASSES Thursday, January 28, 2010
  13. 13. BASE DA OO Thursday, January 28, 2010
  14. 14. MOLDES Thursday, January 28, 2010
  15. 15. INSTRUÇÕES Thursday, January 28, 2010
  16. 16. SEM VONTADE PRÓPRIA Thursday, January 28, 2010
  17. 17. CLASSES Thursday, January 28, 2010
  18. 18. CLASSES Thursday, January 28, 2010
  19. 19. CLASSES Thursday, January 28, 2010
  20. 20. CLASSES Thursday, January 28, 2010
  21. 21. • Uma classe por arquivo • Padrões de nomenclatura • Padrão_Zend_ou_Pear CLASSES • PadrãoCamelCase • PaDrÃoMiGuXo Thursday, January 28, 2010
  22. 22. OBJETOS Thursday, January 28, 2010
  23. 23. FILHOS DAS CLASSES Thursday, January 28, 2010
  24. 24. INFORMAÇÕES Thursday, January 28, 2010
  25. 25. INDEPENDENTES Thursday, January 28, 2010
  26. 26. EXECUTORES Thursday, January 28, 2010
  27. 27. EXECUTAM Thursday, January 28, 2010
  28. 28. ATRIBUTOS Thursday, January 28, 2010
  29. 29. DECLARADOS NA CLASSE Thursday, January 28, 2010
  30. 30. USADOS NOS OBJETOS Thursday, January 28, 2010
  31. 31. PROPRIEDADES Thursday, January 28, 2010
  32. 32. INFORMAÇÕES Thursday, January 28, 2010
  33. 33. VARIÁVEIS Thursday, January 28, 2010
  34. 34. 4 Thursday, January 28, 2010
  35. 35. VARIÁVEIS Thursday, January 28, 2010
  36. 36. 5 Thursday, January 28, 2010
  37. 37. VARIÁVEIS Thursday, January 28, 2010
  38. 38. MÉTODOS Thursday, January 28, 2010
  39. 39. DECLARADOS NA CLASSE Thursday, January 28, 2010
  40. 40. USADOS PELOS OBJETOS Thursday, January 28, 2010
  41. 41. FUNÇÕES Thursday, January 28, 2010
  42. 42. FAZEM COISAS Thursday, January 28, 2010
  43. 43. DÃO E RECEBEM Thursday, January 28, 2010
  44. 44. 4 Thursday, January 28, 2010
  45. 45. DÃO E RECEBEM Thursday, January 28, 2010
  46. 46. 5 Thursday, January 28, 2010
  47. 47. DÃO E RECEBEM Thursday, January 28, 2010
  48. 48. AUTOLOAD Thursday, January 28, 2010
  49. 49. AUTOLOAD •Executado na instanciação •Executado somente para classes não declaradas •Nativo do PHP •Duas implementações •__autoload •SPL •Sem includes/requires •Melhora consumo de memória Thursday, January 28, 2010
  50. 50. AUTOLOAD Thursday, January 28, 2010
  51. 51. AUTOLOAD Thursday, January 28, 2010
  52. 52. MEMBROS DA CLASSE Thursday, January 28, 2010
  53. 53. CONSTANTES Thursday, January 28, 2010
  54. 54. CONSTANTES Thursday, January 28, 2010
  55. 55. ESTÁTICOS Thursday, January 28, 2010
  56. 56. ESTÁTICOS Thursday, January 28, 2010
  57. 57. ESTÁTICOS Thursday, January 28, 2010
  58. 58. MENSAGENS Thursday, January 28, 2010
  59. 59. PODER DA OO Thursday, January 28, 2010
  60. 60. TROCA DE INFORMAÇÕES Thursday, January 28, 2010
  61. 61. TROCA DE INFORMAÇÕES Thursday, January 28, 2010
  62. 62. HERANÇA Thursday, January 28, 2010
  63. 63. CLASSE PAI Thursday, January 28, 2010
  64. 64. CLASSE FILHA Thursday, January 28, 2010
  65. 65. VISIBILIDADE Thursday, January 28, 2010
  66. 66. PUBLIC Thursday, January 28, 2010
  67. 67. PROTECTED Thursday, January 28, 2010
  68. 68. PRIVATE Thursday, January 28, 2010
  69. 69. POLIMORFISMO Thursday, January 28, 2010
  70. 70. ERA UMA COISA Thursday, January 28, 2010
  71. 71. AGORA É OUTRA Thursday, January 28, 2010
  72. 72. SOBRECARGA Thursday, January 28, 2010
  73. 73. NA VERDADE: SOBRESCRITA Thursday, January 28, 2010
  74. 74. NA VERDADE: SOBRESCRITA Thursday, January 28, 2010
  75. 75. FINAL Thursday, January 28, 2010
  76. 76. IMPEDE O POLIMORFISMO Thursday, January 28, 2010
  77. 77. USADO EM CLASSES ... Thursday, January 28, 2010
  78. 78. E MÉTODOS Thursday, January 28, 2010
  79. 79. E MÉTODOS Thursday, January 28, 2010
  80. 80. ABSTRATO Thursday, January 28, 2010
  81. 81. NÃO PODE SER INSTANCIADA Thursday, January 28, 2010
  82. 82. USADA COMO PAI Thursday, January 28, 2010
  83. 83. USADA COMO PAI Thursday, January 28, 2010
  84. 84. USADA COMO PAI Thursday, January 28, 2010
  85. 85. MÁGICA Thursday, January 28, 2010
  86. 86. MÉTODOS MÁGICOS Thursday, January 28, 2010
  87. 87. EXECUTADOS PELO PHP Thursday, January 28, 2010
  88. 88. 14 EXISTENTES NO PHP 5.3 Thursday, January 28, 2010
  89. 89. SEMPRE COMEÇAM COM “_ _” Thursday, January 28, 2010
  90. 90. CONSTRUINDO Thursday, January 28, 2010
  91. 91. __CONSTRUCT •Executado na instanciação do objeto •Não pode retornar nada •Pode sofrer sobrecarga •Pode receber argumentos Thursday, January 28, 2010
  92. 92. __CONSTRUCT Thursday, January 28, 2010
  93. 93. DESTRUINDO Thursday, January 28, 2010
  94. 94. •Executado quando objeto sai da memória •Final do script •unset() do objeto •Pode ser útil em •Banco de dados •Logs •etc ... __DESTRUCT Thursday, January 28, 2010
  95. 95. __DESTRUCT Thursday, January 28, 2010
  96. 96. MOSTRANDO Thursday, January 28, 2010
  97. 97. __TOSTRING •Chamado quando objeto é usado como String •O método DEVE retornar uma String •Útil para objetos que devem retornar algo visual •Paginador, Botão •Até a versão 5.2 funcionava só com `print` e `echo` •> 5.2 funciona em qualquer contexto de string •TypeCast •sprintf() Thursday, January 28, 2010
  98. 98. __TOSTRING Thursday, January 28, 2010
  99. 99. OUTROS MÉTODOS MÁGICOS •__call •__get •__set •__get •__isset •__clone •http://php.net/manual/en/language.oop5.magic.php Thursday, January 28, 2010
  100. 100. INTERFACES Thursday, January 28, 2010
  101. 101. “CLASSES” GENÉRICAS Thursday, January 28, 2010
  102. 102. DEFINEM COMPORTAMENTOS Thursday, January 28, 2010
  103. 103. TIPOS DE DADOS Thursday, January 28, 2010
  104. 104. TIPOS DE DADOS Thursday, January 28, 2010
  105. 105. •Mais genéricos que Classes Abstratas •Uma classe pode implementar várias Interfaces TIPOS DE DADOS Thursday, January 28, 2010
  106. 106. EXCEPTIONS Thursday, January 28, 2010
  107. 107. TRABALHANDO Thursday, January 28, 2010
  108. 108. OBJETOS E REFERÊNCIAS Thursday, January 28, 2010
  109. 109. OBJETOS E REFERÊNCIAS Thursday, January 28, 2010
  110. 110. OBJETOS E REFERÊNCIAS Thursday, January 28, 2010
  111. 111. OBJETOS E REFERÊNCIAS Thursday, January 28, 2010
  112. 112. TYPE HINT Thursday, January 28, 2010
  113. 113. •Forçam um tipo a um argumento •Só podem ser usados como tipo: •Array •Classes TYPE HINT Thursday, January 28, 2010
  114. 114. TYPE HINT Thursday, January 28, 2010
  115. 115. TYPE HINT Thursday, January 28, 2010
  116. 116. INSTANCE OF Thursday, January 28, 2010
  117. 117. •Checa se um objeto é uma determinada instância •Retorna boolean •Um objeto pode ser checado contra •Classe •Classe Abstrata •Interface INSTANCE OF Thursday, January 28, 2010
  118. 118. INSTANCE OF Thursday, January 28, 2010
  119. 119. BANCO DE DADOS Thursday, January 28, 2010
  120. 120. ABSTRAIA Thursday, January 28, 2010
  121. 121. •Melhora segurança •Centraliza execuções •Melhor manutenção •Evita dor de cabeças ABSTRAIA Thursday, January 28, 2010
  122. 122. NÃO REINVENTE A RODA Thursday, January 28, 2010
  123. 123. USE A PDO Thursday, January 28, 2010
  124. 124. •O que ela é: •Abstração de Acesso para banco de dados •Nativo do PHP 5.1 •Aprenda uma sintaxe de conexão •Muito/Extremamente TESTADA pra caramba •Mais leve do que qualquer outra USE A PDO Thursday, January 28, 2010
  125. 125. •O que ela NÃO é: •Não abstrai suas queries •Não evita instalação de drivers dos bancos •Não vai resolver seus problemas •Nem de segurança •Nem de performance USE A PDO Thursday, January 28, 2010
  126. 126. USANDO A PDO Thursday, January 28, 2010
  127. 127. USANDO A PDO Thursday, January 28, 2010
  128. 128. •Agilizam a execução de queries repetidas •Implementam um pouco de segurança •Simplificam o uso de queries repetidas •Simplificam Fetch de resultados PDO STATEMENTS Thursday, January 28, 2010
  129. 129. PDO STATEMENTS Thursday, January 28, 2010
  130. 130. PARÂMETROS Thursday, January 28, 2010
  131. 131. RETORNO ID INSERIDO Thursday, January 28, 2010
  132. 132. DESIGN PATTERNS Thursday, January 28, 2010
  133. 133. •Solução comum para problemas comuns •Independem de linguagem •Vocuabulário comum •Primeiros DP: Gang of Four DESIGN PATTERNS Thursday, January 28, 2010
  134. 134. •Singleton •Factory •Strategy •Data Access Object •Value Objects •MVC DESIGN PATTERNS Thursday, January 28, 2010
  135. 135. FRAMEWORKS Thursday, January 28, 2010
  136. 136. Augusto Pascutti augusto@phpsp.org.br @augustohp http://www.slideshare.net/augustopascutti PERGUNTAS ? Thursday, January 28, 2010

×