Issue in PHP overloading -


<?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

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -