首頁  >  文章  >  後端開發  >  php中 __call 與 __callStatic用法與差異詳解

php中 __call 與 __callStatic用法與差異詳解

伊谢尔伦
伊谢尔伦原創
2017-07-03 11:40:232299瀏覽

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