说句实话哈,对PHP的面向对象特性很不熟悉,不过也知道PHP5里是不能像JAVA,C++,C#等一样的基于参数个数和参数类型的方法重载的,不过看到有本书上实现了基于参数个数的方法重载,只是不是直接的。
看代码吧:
<?php
//测试PHP5 中方法重载
//基于参数个数的重载
abstract class OverloadableObject{
function __call($name,$args)
{
$method=$name."_".count($args);
if(!method_exists($this,$method))
{
throw new Exception("Call to undefined method".get_class($this)."::$method");
}
return call_user_func_array(array($this,$method),$args);
}
}
class Multiplier extends OverloadableObject
{
function multiplier_2($one,$two)
{
return $one*$two;
}
function multiplier_3($one,$two,$three)
{
return $one*$two-$three;
}
}
$aa=new Multiplier();
echo $aa->multiplier(2,3);
echo "<br />";
echo $aa->multiplier(2,3,5);
?>