首页  >  文章  >  后端开发  >  php中 __call 与 __callStatic用法与区别详解

php中 __call 与 __callStatic用法与区别详解

伊谢尔伦
伊谢尔伦原创
2017-07-03 11:40:232305浏览

php 5.3 后新增了 callcallStatic 魔法方法。

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

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

call($funcname, $arguments)
callStatic($funcname, $arguments)

参数说明:

$funcname String 调用的方法名称。

$arguments Array 调用方法时所带的参数。

call 例子

<?php

class Member{

    protected $memberdata = array();

    public function call($func, $arguments){
        list($type, $name) = explode(&#39;_&#39;, $func);
    
        if(!in_array($type, array(&#39;set&#39;,&#39;get&#39;)) || $name==&#39;&#39;){
            return &#39;&#39;;
        }

        switch($type){
            case &#39;set&#39;:
                $this->memberdata[$name] = $arguments[0];
                break;
            
            case &#39;get&#39;:
                return isset($this->memberdata[$name])? $this->memberdata[$name] : &#39;&#39;;
                break;
            
            default:
        }

    }

}


class User extends Member{

    public function show(){
        if($this->memberdata){
            foreach($this->memberdata as $key=>$member){
                echo $key.&#39;:&#39;.$member.&#39;<br>&#39;;
            }
        }
    }

}


class Profession extends Member{

    public function show(){
        if($this->memberdata){
            foreach($this->memberdata as $key=>$member){
                echo $key.&#39;:&#39;.$member.&#39;<br>&#39;;
            }
        }
    }

}

$userobj = new User();
$userobj->set_name(&#39;fdipzone&#39;);
$userobj->set_age(29);
$userobj->show();

$probj = new Profession();
$probj->set_profession(&#39;IT SERVICE&#39;);
$probj->set_price(2500);
$probj->show();

?>

callStatic例子

<?php

class Member{

    protected static $memberdata = array();

    public static function callStatic($func, $arguments){

        list($type, $name) = explode(&#39;_&#39;, $func);
        
        if(!in_array($type, array(&#39;set&#39;,&#39;get&#39;)) || $name==&#39;&#39;){
            return &#39;&#39;;
        }

        $feature = get_called_class();

        switch($type){
            case &#39;set&#39;:
                self::$memberdata[$feature][$name] = $arguments[0];
                break;

            case &#39;get&#39;:
                return isset(self::$memberdata[$feature][$name])? self::$memberdata[$feature][$name] : &#39;&#39;;
                break;

            default:
        }
    
    }

}


class User extends Member{

    public static function show(){

        $feature = get_called_class();

        if(self::$memberdata[$feature]){
            foreach(self::$memberdata[$feature] as $key=>$member){
                echo $key.&#39;:&#39;.$member.&#39;<br>&#39;;
            }
        }
    }

}


class Profession extends Member{

    public static function show(){

        $feature = get_called_class();

        if(self::$memberdata[$feature]){
            foreach(self::$memberdata[$feature] as $key=>$member){
                echo $key.&#39;:&#39;.$member.&#39;<br>&#39;;
            }
        }
    }

}

User::set_name(&#39;fdipzone&#39;);
User::set_age(29);
User::show();

Profession::set_profession(&#39;IT SERVICE&#39;);
Profession::set_price(2500);
Profession::show();

?>

以上是php中 __call 与 __callStatic用法与区别详解的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn