gpt4 book ai didi

php - 函数的神奇函数 __call() ?

转载 作者:行者123 更新时间:2023-12-03 17:41:04 25 4
gpt4 key购买 nike

php 中的神奇函数 __call() 在类中使用。除了函数之外,还有类似的神奇函数吗?就像 __autoload() 用于函数一样。

例如这样的事情

function __call($name, $arguments) {
echo "Function $name says {$arguments[0]} ";
}
random_func("hello");

最佳答案

不,我不认为存在这样的神奇功能。

解决此问题的一种方法是将函数放入静态类中,并添加 __callStatic该类的魔术方法(恐怕仅适用于 PHP 5.3):

class Func
{
/** 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";

}
}

Func::random_func("hello!");

对于 PHP < 5.3,您可以执行相同的操作,但您必须实例化一个对象并使用 __call 魔术方法。

$Func = new Func;
$Func->random_func("hello!");

关于php - 函数的神奇函数 __call() ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3456763/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com