Home > Article > Backend Development > Explanation of php __call and __callStatic
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('_', $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 example<?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();
?>
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 instructionsRelated explanations about PHPMailer - PHP email transport classUnderstanding of PHP traversal of folders, file classes and processing classesThe 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!