php类的重载函数简单的实现

<?php

/*
 * 这样写出来的基类下次就可以直接修改很方便了
 *
 */
class basic_inter{
    private $count;
    private $value;
    private $result;
    function __construct()
    {
        $this->count=0;
    }
    function setValue($cout,$arrs)
    {
        $this->count=$cout;
        $this->value=$arrs;
    }
    private function ChooseFun()
    {
        switch ($this->count){
            case 1:
                return $this-> GetMax_with_One_Parament($this->value[0]);
                break;
            case 2:
                return $this-> GetMax_with_Two_Parament($this->value[0],$this->value[1]);
                break;
            case 3:
                return $this->GetMax_with_Three_Parament($this->value[0],$this->value[1],$this->value[2]);
                break;
//这里你需要什么函数就可以自己去添加
            default:
                return -99999999999;
                break;
        }
    }
    private function adjuage($min,$max)
    {
        return $max>$min?$max:$min;
    }
    private function GetMax_with_One_Parament($value)
    {
        return $value;
    }
    private function GetMax_with_Two_Parament($value1,$value2)
    {
        return $this->adjuage($value1,$value2);
    }
    private function GetMax_with_Three_Parament($value1,$value2,$value3)
    {
        return $this->adjuage($this->adjuage($value1,$value2),$value3);
    }
    function getValues()
    {
        $this->result=$this->ChooseFun();
        return $this->result;
    }
}

class Compare extends basic_inter
{
   private $para;
   private $arr_value;
   private $result;
   function __construct()
   {
       $this->para=0;
   }
   function GetMax()
   {
       $this->arr_value=func_get_args();
       $this->para=func_num_args();
       //获取到了其中的数组的值
       $this->coordinate_function();
   }
   private function coordinate_function()
   {
       $this->setValue($this->para,$this->arr_value);
       $this->result=$this->getValues();
       echo $this->result;
   }

}

$c = new Compare();    //实例化"计算"类
echo "两个数比较示例:";
echo $c->GetMax(12,-26)."<br>";     //调用成员方法,获得输入的数的较大值,输出12
echo "三个数比较示例:";
echo $c->GetMax(12,25,31)."<br>";  //调用成员方法,获得输入的数的较大值,输出31
?>
其实这个是非常简单的,php本身不支持重载的,同时还有简单的方法就是用缺省函数的方法去解决它。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值