PHP魔术方法__callStatic()

2018-09-15 23:00 By "Powerless" 3013 0 2

__callStatic()的用法与__call()类似。示例如下:

<?php
class Person
{
    function say()
    {
        echo "Hello, world!<br>";
    }
    public static function __callStatic($funName, $arguments)
    {
        echo "The static method you called:" . $funName . "(parameter:" ;  // 输出不存在的方法的名称
        print_r($arguments); // 输出不存在的方法的参数列表
        echo ")does not exist!<br>\n";
    }
}
$Person = new Person();
$Person::run("teacher"); // 如果对象内不存在的方法被调用,则 __callStatic() 方法会被自动调用
$Person::eat("John", "apple");
$Person->say();

输出结果如下:

The static method you called: run (parameter: Array([0] => teacher)) does not exist!
The static method you called: eat (parameter: Array([0] => John[1] => apple)) does not exist!
Hello world!

评 论

View in WeChat

Others Discussion

  • HTTP头中隐藏PHP版本号
    Posted on 2021-01-11 16:38
  • Composer 异常 [ErrorException]
    Posted on 2019-11-25 17:55
  • TCP协议的特性
    Posted on 2019-04-26 16:46
  • 能创建多少个 TCP 连接?
    Posted on 2021-08-02 16:00
  • PHP练习-移动数组内的0到最后并保持其他元素顺序不变
    Posted on 2020-08-14 20:32
  • Mysql联合索引的最左前缀匹配原则
    Posted on 2018-08-25 15:00
  • PHP没你想的那么差
    Posted on 2021-12-17 15:40
  • 快速了解Kafka
    Posted on 2021-03-25 14:20