zoukankan      html  css  js  c++  java
  • php中__call() 和 __callStatic方法的使用

    __call 与__callStatic 魔法方法是php5.3后新增的,二者的应用场景:

    1、当要调用的方法不存在或权限不足时,会自动调用__call 方法。

    2、当调用的静态方法不存在或权限不足时,会自动调用__callStatic方法。

    class Person
    {
    
        public function __call($method, $arguments) {
            echo '我是要调用的不存在的动态方法名: ', $method, '<br>';
            echo '以下是通过__call方法显示的参数', '<br>';
            var_dump($arguments);
        }
    
         public static function __callStatic($method, $arguments) {
            echo '我是要调用的不存在的静态方法名: ', $method, '<br>';
            echo '以下是通过__callStatic方法显示的参数', '<br>';
            var_dump($arguments);
         }
    }
    
    // 调用对象不存在的方法hello
    (new Person())->hello(['a', 'b'], ['c', 'd']);
    echo '<hr>';
    // 调用类不存在的静态方法world
    Person::world(['e', 'f'], ['g', 'h']);

    输出如下:

    我是要调用的不存在的动态方法名: hello
    以下是通过__call方法显示的参数
    D:wamp64www	estcall.php:9:
    array (size=2)
      0 => 
        array (size=2)
          0 => string 'a' (length=1)
          1 => string 'b' (length=1)
      1 => 
        array (size=2)
          0 => string 'c' (length=1)
          1 => string 'd' (length=1)
    我是要调用的不存在的静态方法名: world
    以下是通过__callStatic方法显示的参数
    D:wamp64www	estcall.php:15:
    array (size=2)
      0 => 
        array (size=2)
          0 => string 'e' (length=1)
          1 => string 'f' (length=1)
      1 => 
        array (size=2)
          0 => string 'g' (length=1)
          1 => string 'h' (length=1)
  • 相关阅读:
    回流与重绘
    事件循环的一些小啰嗦
    async与await的那些事儿
    数组去重的方法们
    Object的一些常用方法
    JS事件流的一些理解
    关于Ajax的那些记录
    call、bind与apply函数的区别
    继承的一些细碎记载
    计算机基础的小贴士(1)
  • 原文地址:https://www.cnblogs.com/splendid/p/10657941.html
Copyright © 2011-2022 走看看