PHP魔术方法__callStatic()

2018-09-15 23:00 By "Powerless" 2902 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!

评 论

Others Discussion

  • 分布式服务限流
    Posted on 2020-02-07 18:57
  • 有状态服务VS无状态服务
    Posted on 2020-02-07 18:18
  • 企业级PAAS云平台几个关键问题和挑战
    Posted on 2019-06-12 18:33
  • MySQL 单库后期分库策略
    Posted on 2019-08-19 14:31
  • Redis七大经典问题
    Posted on 2021-05-27 11:14
  • 关于HTTPS的五大误区
    Posted on 2020-02-02 01:10
  • PHP扩展GD安装
    Posted on 2018-10-29 18:29
  • PHP实现精确发布时间
    Posted on 2018-12-06 21:00