Home > Backend Development > PHP Tutorial > PHP magic method __call and __callStatic methods

PHP magic method __call and __callStatic methods

WBOY
Release: 2016-08-08 09:19:13
Original
994 people have browsed it
    <?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'));  
    */   
      
    ?>  
Copy after login

Copyright Statement: This article is an original article by the blogger and may not be reproduced without the blogger's permission.

The above has introduced the __call and __callStatic methods of PHP magic methods, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
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