Issue in PHP overloading -
this question has answer here:
<?php class a{ function __call($name,$num){ echo "method name:".$name."<p>"; echo "the number of parameter:".count($num)."<p>"; if(count($num)==1){ echo $this-> list1($a); } if(count($num)==2){ echo $this-> list2($a,$b); } } public function list1($a){ return "this function list1"; } public function list2($a,$b){ return "this function list1"; } } (new a)->listshow(1,2); ?>
(php overload) set 2 functions class member choose. have mistake, show
undefined variable: on line 10.
why?
you're trying call list2()
within __call()
using args $a
, $b
these aren't defined anywhere in __call()
instead, need pass $num
array.... i'd prefer call $args
... , use ...
operator argument packing
class a{ function __call($name, $args){ echo "method name:".$name."<p>"; echo "the number of parameter:".count($args)."<p>"; if(count($args)==1){ echo $this-> list1(...$args); } if(count($args)==2){ echo $this-> list2(...$args); } } public function list1($a){ return "this function list1 $a"; } public function list2($a,$b){ return "this function list2 $a , $b"; } } (new a)->listshow(1,2);
Comments
Post a Comment