__call()函数是php类的默认魔法函数,__call() 在一个对象的上下文中,如果调用的方法不存在的时候,它将被触发:<?php
class Call
{
//某个已经有的方法
public function callIt($arg)
{
echo 'Echo... '.$arg.PHP_EOL;
}
//__call方法
public function __call($name, $args)
{
if(substr($name, 0, 4) == 'call'){
echo 'No '.$name.', so use callIt'.PHP_EOL;
self::callIt($args[0]);
}else{
echo 'Sorry, "'.$name.'" not found.'.PHP_EOL;
}
}
} // End Call
$call = new Call();
$call->callIt('first');
// Echo... first
$call->callSomething('second');
// No callSomething, so use callIt
// Echo... second
$call->another('something');
// Sorry "anther" not found
class Call
{
//某个已经有的方法
public function callIt($arg)
{
echo 'Echo... '.$arg.PHP_EOL;
}
//__call方法
public function __call($name, $args)
{
if(substr($name, 0, 4) == 'call'){
echo 'No '.$name.', so use callIt'.PHP_EOL;
self::callIt($args[0]);
}else{
echo 'Sorry, "'.$name.'" not found.'.PHP_EOL;
}
}
} // End Call
$call = new Call();
$call->callIt('first');
// Echo... first
$call->callSomething('second');
// No callSomething, so use callIt
// Echo... second
$call->another('something');
// Sorry "anther" not found