Heim  >  Artikel  >  Backend-Entwicklung  >  PHP魔术方法之__call与__callStatic方法

PHP魔术方法之__call与__callStatic方法

WBOY
WBOYOriginal
2016-08-08 09:19:13922Durchsuche
    <?php class aaa{  
      
    private function t(){  
      }  
      
    //魔术方法__call  
    /* 
    $method 获得方法名 
    $arg 获得方法的参数集合 
    */  
    public function __call($method,$arg){  
        echo &#39;不存在的方法&#39;,$method,&#39;方法<br/>';  
        echo '不存在方法中有参数传入<br>';  
        echo print_r($arg),'<br>';  
      }  
    //魔术方法__callStatic
    public static function __callStatic($method,$arg){  
      
        echo '不存在的',$method,'静态方法<br>';  
        echo '还传了一个参数<br>';  
        echo print_r($arg),'<br>';  
      }  
      
    }  
      
    $a=new aaa(); 
    $a->xx(1,2,3);  
    /* 
    调用一个未定义的方法 
    Fatal error: Call to undefined method aaa::xx() in D:\wamp\www\php\aaa.php on line 8 
    */  
      
    $li->t('a','b');  
    /*  
    __call是调用不可见(不存在或无权限)的方法时,自动调用  
    $a->xx(1,2,3);-----没有xx()方法----> __call('xx',array(1,2,3))运行  
    */   
      
    aaa::yy('a','b','c'); 
    /*  
    __callStatic 是调用不可见的静态方法时,自动调用.  
    aaa::yy('a','b','c')----没有yy方法---> aaa::__callStatic('yy',array('a','b','c'));  
    */   
      
    ?>  

版权声明:本文为博主原创文章,未经博主允许不得转载。

以上就介绍了PHP魔术方法之__call与__callStatic方法,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Vorheriger Artikel:UTF-8汉字正则表达式Nächster Artikel:Nginx 反向代理 Demo