Explanation of php __call and __callStatic

jacklove
Release: 2023-03-31 14:06:01
Original
1743 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

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.'
'; } } } } class Profession extends Member{ public function show(){ if($this->memberdata){ foreach($this->memberdata as $key=>$member){ echo $key.':'.$member.'
'; } } } } $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(); ?>
Copy after login

__callStatic example

$member){
                echo $key.':'.$member.'
'; } } } } 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.'
'; } } } } User::set_name('fdipzone'); User::set_age(29); User::show(); Profession::set_profession('IT SERVICE'); 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
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!