php 5.3 后新增了 __call 与__callStatic 魔法方法。

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

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

__call($funcname, $arguments)

__callStatic($funcname, $arguments)


参数说明:

$funcname String 调用的方法名称。

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

__call 例子

  1. <?php
  2. class Member{
  3.     protected $memberdata = array();
  4.     public function __call($func, $arguments){
  5.         list($type, $name) = explode(‘_’, $func);
  6.         if(!in_array($type, array(‘set’,’get’)) || $name==”){
  7.             return ”;
  8.         switch($type){
  9.             case ‘set’:
  10.                 $this->memberdata[$name] = $arguments[0];
  11.                 break;
  12.             case ‘get’:
  13.                 return isset($this->memberdata[$name])? $this->memberdata[$name] : ”;
  14.                 break;
  15.             default:
  16. class User extends Member{
  17.     public function show(){
  18.         if($this->memberdata){
  19.             foreach($this->memberdata as $key=>$member){
  20.                 echo $key.’:’.$member.'<br>’;
  21. class Profession extends Member{
  22.     public function show(){
  23.         if($this->memberdata){
  24.             foreach($this->memberdata as $key=>$member){
  25.                 echo $key.’:’.$member.'<br>’;
  26. $userobj = new User();
  27. $userobj->set_name(‘fdipzone’);
  28. $userobj->set_age(29);
  29. $userobj->show();
  30. $probj = new Profession();
  31. $probj->set_profession(‘IT SERVICE’);
  32. $probj->set_price(2500);
  33. $probj->show();
  34. ?>
<?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例子

  1. <?php
  2. class Member{
  3.     protected static $memberdata = array();
  4.     public static function __callStatic($func, $arguments){
  5.         list($type, $name) = explode(‘_’, $func);
  6.         if(!in_array($type, array(‘set’,’get’)) || $name==”){
  7.             return ”;
  8.         $feature = get_called_class();
  9.         switch($type){
  10.             case ‘set’:
  11.                 self::$memberdata[$feature][$name] = $arguments[0];
  12.                 break;
  13.             case ‘get’:
  14.                 return isset(self::$memberdata[$feature][$name])? self::$memberdata[$feature][$name] : ”;
  15.                 break;
  16.             default:
  17. class User extends Member{
  18.     public static function show(){
  19.         $feature = get_called_class();
  20.         if(self::$memberdata[$feature]){
  21.             foreach(self::$memberdata[$feature] as $key=>$member){
  22.                 echo $key.’:’.$member.'<br>’;
  23. class Profession extends Member{
  24.     public static function show(){
  25.         $feature = get_called_class();
  26.         if(self::$memberdata[$feature]){
  27.             foreach(self::$memberdata[$feature] as $key=>$member){
  28.                 echo $key.’:’.$member.'<br>’;
  29. User::set_name(‘fdipzone’);
  30. User::set_age(29);
  31. User::show();
  32. Profession::set_profession(‘IT SERVICE’);
  33. Profession::set_price(2500);
  34. Profession::show();
  35. ?>
<?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();

?>