<?php
namespace Build;
class Forma
{
public static function getForma($forma, $area)
{
switch($forma):
case 'circulo';
return new Circulo($area);
case 'quadrado':
return new Quadrado($area);
default:
return new stdClass();
endswitch;
}
public function getArea()
{
// return get_class($this)
return $this->getArea();
}
}
class Circulo extends Forma
{
private $area;
public function __construct($area)
{
$this->area = $area;
}
public function getArea(){
return M_PI * ($this->area)**2;
}
}
class Quadrado extends Forma
{
private $area;
public function __construct($area)
{
$this->area = $area;
}
public function getArea()
{
return ($this->area)**2;
}
}
$forma = \Build\Forma::getForma('circulo', 3);
print $forma->getArea();
print "<br/>";
$forma = \Build\Forma::getForma('quadrado', 4);
print $forma->getArea();
print "<br/>";
?>
PD9waHAKCm5hbWVzcGFjZSBCdWlsZDsKCmNsYXNzIEZvcm1hCnsKCXB1YmxpYyBzdGF0aWMgZnVuY3Rpb24gZ2V0Rm9ybWEoJGZvcm1hLCAkYXJlYSkKCXsKCQlzd2l0Y2goJGZvcm1hKToKCQkJY2FzZSAnY2lyY3Vsbyc7CgkJCQlyZXR1cm4gbmV3IENpcmN1bG8oJGFyZWEpOwoJCQljYXNlICdxdWFkcmFkbyc6CgkJCQlyZXR1cm4gbmV3IFF1YWRyYWRvKCRhcmVhKTsKCQkJZGVmYXVsdDoKCQkJCXJldHVybiBuZXcgc3RkQ2xhc3MoKTsJCQkKCQllbmRzd2l0Y2g7CQoJfQoJcHVibGljIGZ1bmN0aW9uIGdldEFyZWEoKQoJewoJCS8vIHJldHVybiBnZXRfY2xhc3MoJHRoaXMpCgkJcmV0dXJuICR0aGlzLT5nZXRBcmVhKCk7Cgl9Cn0KCmNsYXNzIENpcmN1bG8gZXh0ZW5kcyBGb3JtYSAKewoJcHJpdmF0ZSAkYXJlYTsKCXB1YmxpYyBmdW5jdGlvbiBfX2NvbnN0cnVjdCgkYXJlYSkKCXsKCQkkdGhpcy0+YXJlYSA9ICRhcmVhOwoJfQoJcHVibGljIGZ1bmN0aW9uIGdldEFyZWEoKXsKCQlyZXR1cm4gTV9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