咱們都知道,在PHP5中並無真正的支持重載。可是咱們能夠想辦法去實現它,藉助的工具就是__call()。咱們來看例子。php
class Test{ public function __call($fun,$args) { if(method_exists($this, $fun.count($args))){ return call_user_func_array(array(&$this,$fun.count($args)),$args); }else{ throw new Exception('調用了未知的方法:'.get_class($this).'->'.$fun); } } //一個參數的方法 public function fun1($a){ echo '你在調用一個參數的方法,參數爲'.$a; } //兩個參數的方法 public function fun2($a,$b){ echo '你在調用兩個參數的方法,參數爲:'.$a.'和'.$b; } } $test = new Test(); $test->fun('a'); $test->fun('a','b'); $test->fun('q','q','q');
這樣咱們基本就能夠完成了重載了。工具