欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

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

程序员文章站 2022-03-26 16:57:06
...
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('_', $func);
    
        if(!in_array($type, array('set','get')) || $name==''){
            return '';
        }

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

    }

}


class User extends Member{

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

}


class Profession extends Member{

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

}

$userobj = new User();
$userobj->set_name('fdipzone');
$userobj->set_age(29);
$userobj->show();

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

?>

callStatic例子

<?php

class Member{

    protected static $memberdata = array();

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

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

        $feature = get_called_class();

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

            case 'get':
                return isset(self::$memberdata[$feature][$name])? self::$memberdata[$feature][$name] : '';
                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.':'.$member.'<br>';
            }
        }
    }

}


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.':'.$member.'<br>';
            }
        }
    }

}

User::set_name('fdipzone');
User::set_age(29);
User::show();

Profession::set_profession('IT SERVICE');
Profession::set_price(2500);
Profession::show();

?>

以上就是php中 __call 与 __callStatic用法与区别详解的详细内容,更多请关注其它相关文章!