Rumah > pembangunan bahagian belakang > tutorial php > php中 __call 与 __callStatic用法与区别详解

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

伊谢尔伦
Lepaskan: 2023-03-12 13:48:01
asal
2346 orang telah melayarinya

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

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

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

call($funcname, $arguments)
callStatic($funcname, $arguments)
Salin selepas log masuk

参数说明:

$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();

?>
Salin selepas log masuk

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();

?>
Salin selepas log masuk

Atas ialah kandungan terperinci php中 __call 与 __callStatic用法与区别详解. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Label berkaitan:
php
sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan