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

How to call non-existent method in php

藏色散人
Release: 2023-03-06 15:00:02
Original
2521 people have browsed it

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;
   }
}
Copy after login

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!

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template