码迷,mamicode.com
首页 > Web开发 > 详细

PHP工厂模式计算面积与周长

时间:2018-10-10 17:07:29      阅读:189      评论:0      收藏:0      [点我收藏+]

标签:efault   const   pow   bre   指定   cto   str   ret   tar   

<?php
interface InterfaceShape
{
function getArea();
function getCircumference();
}

/**
* 矩形
*/
class Rectangle implements InterfaceShape
{
private $width;
private $height;
public function __construct($width,$height){
$this->width=$width;
$this->height=$height;
}
public function getArea(){
return $this->width*$this->height;
}
public function getCircumference(){
return 2*$this->width+2*$this->height;
}
}
/**
* 圆
*/
class circle implements InterfaceShape{
private $redius;
public function __construct($redius){
$this->redius = $redius;
}
public function getArea(){
return M_PI*pow($this->redius,2);
}
public function getCircumference(){
return M*PI*2*$this->redius;
}
}
class FactoryShape{//func_get_arg(索引)返回当前函数指定索引的参数值,返回string格式;
static public function create(){
switch (func_num_args()) {
case 1:
return new circle(func_get_arg(0));
case 2:
return new Rectangle(func_get_arg(0),func_get_arg(1));
default:
break;
}
}
}
$rect = FactoryShape::create(5,5);
var_dump($rect);
echo "<br>";
$circle = FactoryShape::create(4);
var_dump($circle);
?>

PHP工厂模式计算面积与周长

标签:efault   const   pow   bre   指定   cto   str   ret   tar   

原文地址:https://www.cnblogs.com/isuansuan/p/9767105.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!