1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
| interface Shape { public function getCircum();
public function getArea(); }
class Rectangle implements Shape {
private $width, $height;
public function __construct($width, $height) { $this->width = $width; $this->height = $height; }
public function getCircum() {
return ($this->width + $this->height) * 2; }
public function getArea() {
return $this->width * $this->height; } }
class Circle implements Shape {
private $radii;
public function __construct($radii) { $this->radii = $radii; }
public function getCircum() { return 2 * M_PI * $this->radii;
}
public function getArea() { return M_PI * pow($this->radii, 2); } }
class Factory {
public static function create() { switch (func_num_args()) { case 1: return new Circle(func_get_arg(0)); break; case 2: return new Rectangle(func_get_arg(0), func_get_arg(1)); break; }
} }
$rectangle = Factory::create(4,2); echo $rectangle->getCircum()." "; echo $rectangle->getArea()." ";
$circle = Factory::create(3); echo $circle->getCircum()." "; echo $circle->getArea();
|