fork download
  1. <?php
  2.  
  3. namespace Build;
  4.  
  5. class Forma
  6. {
  7. public static function getForma($forma, $area)
  8. {
  9. switch($forma):
  10. case 'circulo';
  11. return new Circulo($area);
  12. case 'quadrado':
  13. return new Quadrado($area);
  14. default:
  15. return new stdClass();
  16. endswitch;
  17. }
  18. public function getArea()
  19. {
  20. // return get_class($this)
  21. return $this->getArea();
  22. }
  23. }
  24.  
  25. class Circulo extends Forma
  26. {
  27. private $area;
  28. public function __construct($area)
  29. {
  30. $this->area = $area;
  31. }
  32. public function getArea(){
  33. return M_PI * ($this->area)**2;
  34. }
  35. }
  36.  
  37. class Quadrado extends Forma
  38. {
  39. private $area;
  40. public function __construct($area)
  41. {
  42. $this->area = $area;
  43. }
  44. public function getArea()
  45. {
  46. return ($this->area)**2;
  47. }
  48. }
  49.  
  50. $forma = \Build\Forma::getForma('circulo', 3);
  51. print $forma->getArea();
  52. print "<br/>";
  53. $forma = \Build\Forma::getForma('quadrado', 4);
  54. print $forma->getArea();
  55. print "<br/>";
  56.  
  57. ?>
Success #stdin #stdout 0.03s 52480KB
stdin
Standard input is empty
stdout
28.274333882308<br/>16<br/>