Home >Backend Development >PHP Problem >How to call non-existent method in php
php calls a non-existing method: first create a PHP sample file; then set the static method name of the pseudo method; then use the "$funArr" array to store the mapping relationship between the pseudo method and the real non-static method; Finally, return the real method processing result.
Recommended: "PHP Video Tutorial"
php Access non-existent static methods through __callstatic and map To the real method
<?php //调用不存在的静态方法name,映射到真正的output方法 echo A::name('巴拉巴拉'); class A { //$name为伪方法的静态方法名,$args为传递的参数 public static function __callStatic($name,$args) { // $funArr数组存放伪方法与真实非非静态方法之间的映射关系 $funArr=['name'=>'output', 'email'=>'email']; if(array_key_exists($name,$funArr)){ //把真实方法名赋给$method $method=$funArr[$name]; //返回真实方法处理结果 return (new who())->$method($args[0]); }else{ return "unknown function name".$name; } } public function output($name) { return 'your name is '.$name; } }
The above is the detailed content of How to call non-existent method in php. For more information, please follow other related articles on the PHP Chinese website!