你应该在
method overriding(你的例子)和
method overloading之间做出区分
这是一个简单的例子,说明如何使用__call魔术方法在PHP中实现方法重载:
class test{
public function __call($name, $arguments)
{
if ($name === 'test'){
if(count($arguments) === 1 ){
return $this->test1($arguments[0]);
}
if(count($arguments) === 2){
return $this->test2($arguments[0], $arguments[1]);
}
}
}
private function test1($data1)
{
echo $data1;
}
private function test2($data1,$data2)
{
echo $data1.' '.$data2;
}
}
$test = new test();
$test->test('one argument'); //echoes "one argument"
$test->test('two','arguments'); //echoes "two arguments"