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

来源:互联网 发布:浙江软件考试报名 编辑:程序博客网 时间:2024/06/08 02:49

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


原创粉丝点击