php的魔术方法_call()在调用类中未定义的方法时会自动调用。php.net中代码示例如下:
<?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
?>
本文介绍了PHP中的魔术方法_call的功能及使用方式。当尝试调用一个类中未定义的方法时,_call方法会被自动调用。文章通过示例代码展示了如何实现这一特性,并解释了注意事项。
346

被折叠的 条评论
为什么被折叠?



