fork download
  1. <?php
  2.  
  3. class Usuario
  4. {
  5. private $nome;
  6. private $profissao;
  7.  
  8. public function setNome($nome)
  9. {
  10. $this->nome = $nome;
  11. return $this;
  12. }
  13.  
  14. public function getNome($nome)
  15. {
  16. return $this->nome;
  17. }
  18.  
  19. public function setProfissao($profissao)
  20. {
  21. $this->profissao = $profissao;
  22. return $this;
  23. }
  24.  
  25. public function getProfissao($profissao)
  26. {
  27. return $this->profissao;
  28. }
  29.  
  30. function getValueByAttributeName($name)
  31. {
  32. if (property_exists($this, $name)) {
  33. return $this->$name;
  34. }
  35. }
  36.  
  37. function getValueByMethodName($name)
  38. {
  39. if (method_exists($this, $name)) {
  40. return $this->$name();
  41. }
  42. }
  43.  
  44. function getAllAttributes()
  45. {
  46. $array = array();
  47.  
  48. foreach ($this as $key => $value) {
  49. if (property_exists($this, $key)) {
  50. $array[] = $value;
  51. }
  52. }
  53. return $array;
  54.  
  55. }
  56. }
  57.  
  58. $usuario = new Usuario();
  59.  
  60. $usuario->setNome('Nome Qualquer');
  61. $usuario->setProfissao('Profissão Qualquer');
  62. $data = $usuario->getAllAttributes();
  63.  
  64. echo '<pre>';
  65. print_r($data);
  66. echo '</pre><br>';
  67. echo $usuario->getValueByMethodName('getnome');
  68. echo '<br>';
  69. echo $usuario->getValueByAttributeName('nome');
Success #stdin #stdout #stderr 0.03s 52480KB
stdin
Standard input is empty
stdout
<pre>Array
(
    [0] => Nome Qualquer
    [1] => Profissão Qualquer
)
</pre><br>Nome Qualquer<br>Nome Qualquer
stderr
PHP Warning:  Missing argument 1 for Usuario::getNome(), called in /home/PCWzwV/prog.php on line 40 and defined in /home/PCWzwV/prog.php on line 14