将常量称为函数名称?

时间:2023-01-05 16:45:47

(Please read the whole question before telling me any new ways to do it.)

(请在告诉我任何新方法之前先阅读整个问题。)

In PHP, you can call functions by calling their name inside a variable.

在PHP中,您可以通过在变量中调用它们的名称来调用函数。

function myfunc(){ echo 'works'; }

$func = 'myfunc';
$func(); // Prints "works"

But, you can't do this with constants.

但是,你不能用常数做到这一点。

define('func', 'myfunc');

func(); // Error: function "func" not defined

There are workarounds, like these:

有一些解决方法,如下所示:

$f = func;
$f(); // Prints "works"

call_user_func(func); // Prints "works"

function call($f){ $f(); }
call(func); // Prints "works"

The PHP documentation on callable says:

关于callable的PHP文档说:

A PHP function is passed by its name as a string. Any built-in or user-defined function can be used, except language constructs.

PHP函数的名称作为字符串传递。除语言结构外,可以使用任何内置或用户定义的函数。

There seems to be nothing about constant values not being callable.

似乎没有关于常量值不可调用的内容。

I also tried to check it, and of course,

我也尝试检查它,当然,

var_dump(is_callable(func));

prints bool(true).

Now, is there an explanation as to why is it this way? As far as I can see all the workarounds rely on assigning the constant value to a variable, by why can't constant be called?

现在,有没有解释为什么会这样?据我所知,所有的变通方法都依赖于为变量赋值常量,为什么不能连续调用?

And again, just to make it super clear, I don't need a way to call the function, I even presented some there. I want to know why PHP doesn't allow calling the function directly through the constant.

再说一次,为了让它变得非常清晰,我不需要一种方法来调用函数,我甚至在那里展示了一些。我想知道为什么PHP不允许直接通过常量调用函数。

2 个解决方案

#1


1  

Since you asked for the why/reason I guess the only answer (which will probably not satisfy you) is:
Because it hasn't been proposed, discussed and accepted on https://wiki.php.net/rfc .

既然你问的原因/原因我猜唯一的答案(可能不会让你满意)是:因为它没有在https://wiki.php.net/rfc上提出,讨论和接受。

#2


0  

When trying to call a constant as a function, like so:

当试图将常量作为函数调用时,如下所示:

define('FUNC', 'myFunction');

function myFunction()
{
    echo 'Oh, hello there.';
}

FUNC(); // Fatal error: Call to undefined function FUNC()

Will cause the PHP Interpreter to expect a function called FUNC. Hence the error Fatal error: Call to undefined function FUNC()

将导致PHP解释器期望一个名为FUNC的函数。因此错误致命错误:调用未定义函数FUNC()

Despite the type of the constant FUNC being callable, calling it as a function won't work. Simply because PHP expects a function named FUNC.

尽管常量FUNC的类型是可调用的,但将其作为函数调用将不起作用。只是因为PHP需要一个名为FUNC的函数。

So without a workaround, you won't be able to call a function directly through a constant.

因此,如果没有解决方法,您将无法通过常量直接调用函数。

#1


1  

Since you asked for the why/reason I guess the only answer (which will probably not satisfy you) is:
Because it hasn't been proposed, discussed and accepted on https://wiki.php.net/rfc .

既然你问的原因/原因我猜唯一的答案(可能不会让你满意)是:因为它没有在https://wiki.php.net/rfc上提出,讨论和接受。

#2


0  

When trying to call a constant as a function, like so:

当试图将常量作为函数调用时,如下所示:

define('FUNC', 'myFunction');

function myFunction()
{
    echo 'Oh, hello there.';
}

FUNC(); // Fatal error: Call to undefined function FUNC()

Will cause the PHP Interpreter to expect a function called FUNC. Hence the error Fatal error: Call to undefined function FUNC()

将导致PHP解释器期望一个名为FUNC的函数。因此错误致命错误:调用未定义函数FUNC()

Despite the type of the constant FUNC being callable, calling it as a function won't work. Simply because PHP expects a function named FUNC.

尽管常量FUNC的类型是可调用的,但将其作为函数调用将不起作用。只是因为PHP需要一个名为FUNC的函数。

So without a workaround, you won't be able to call a function directly through a constant.

因此,如果没有解决方法,您将无法通过常量直接调用函数。