Home >Backend Development >PHP Problem >How to call non-existent method in php

How to call non-existent method in php

藏色散人
藏色散人Original
2020-10-09 10:35:542519browse

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.

How to call non-existent method in php

Recommended: "PHP Video Tutorial"

php Access non-existent static methods through __callstatic and map To the real method

<?php
//调用不存在的静态方法name,映射到真正的output方法
echo A::name(&#39;巴拉巴拉&#39;);
class A
{
    //$name为伪方法的静态方法名,$args为传递的参数
    public static function __callStatic($name,$args)
    {
        // $funArr数组存放伪方法与真实非非静态方法之间的映射关系
        $funArr=[&#39;name&#39;=>&#39;output&#39;, &#39;email&#39;=>&#39;email&#39;];
        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  &#39;your name is &#39;.$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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn