官方文檔
https://www.php.net/manual/en/language.oop5.overloading.php#object.call
官方示例
<?php
class MethodTest
{
public function __call($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
/** As of PHP 5.3.0 */
public static function __callStatic($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
}
$obj = new MethodTest;
$obj->runTest('in object context');
MethodTest::runTest('in static context'); // As of PHP 5.3.0
?>
這時(shí)候如果本身有這種方法
<?php
class MethodTest
{
public function __call($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling object method '$name' "
. implode(', ', $arguments). "\n";
}
/** As of PHP 5.3.0 */
public static function __callStatic($name, $arguments)
{
// Note: value of $name is case sensitive.
echo "Calling static method '$name' "
. implode(', ', $arguments). "\n";
}
public function runTest() {
echo "office runTest";
}
}
$obj = new MethodTest;
$obj->runTest('in object context');
// MethodTest::runTest('in static context'); // As of PHP 5.3.0
?>
則不會(huì)執(zhí)行 __call
結(jié)論:
__call 作為魔術(shù)方法當(dāng)某個(gè)類執(zhí)行了沒(méi)有實(shí)現(xiàn)的方法時(shí), 會(huì)去執(zhí)行 __call .