Detailed explanation of the usage and difference between __call and __callStatic in php

伊谢尔伦
Release: 2023-03-12 13:48:01
Original
2262 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)
Copy after login

Parameter description:

##$funcname String Call method name.

$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

The above is the detailed content of Detailed explanation of the usage and difference between __call and __callStatic in php. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!