Home > Backend Development > PHP Tutorial > Explanation of php __call and __callStatic

Explanation of php __call and __callStatic

jacklove
Release: 2023-03-31 14:06:01
Original
1942 people have browsed it

The __call and __callStatic magic methods were added after php 5.3.

__call When the method to be called does not exist or has insufficient permissions, the __call method will be automatically called.

__callStatic When the called static method does not exist or has insufficient permissions, the __callStatic method will be automatically called.

__call($funcname, $arguments)

##__callStatic($funcname, $arguments)

Parameter description:

##$funcname String The name of the method to be called.

$arguments Array Parameters taken when calling the method.

__call example

<?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();
?>
Copy after login

__callStatic example

<?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();
?>
Copy after login
This article explains about php __call and __callStatic. For more related knowledge, please pay attention to the php Chinese website.

Related recommendations:

About memcached common commands and usage instructions

Related explanations about PHPMailer - PHP email transport class

Understanding of PHP traversal of folders, file classes and processing classes

The above is the detailed content of Explanation of php __call and __callStatic. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template