且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以创建一个PHP函数,我可以在没有括号的情况下调用它吗?

更新时间:2023-09-09 22:50:58

print不是变量函数




因为这是一个语言结构
而不是一个函数,所以它不能被
使用变量函数调用





变量函数



PHP支持变量
函数的概念。这意味着如果一个
变量名的括号附加了
,PHP将查找一个函数
,其名称与
变量的值相同,并且
试图执行它。在其他
的东西中,这可以用来实现
回调,函数表等等



I have a function that is effectively a replacement for print, and I want to call it without parentheses, just like calling print.

# Replace
print $foo, $bar, "\n";

# with
myprint $foo, $bar, "\n";

In Perl, you can create subroutines with parameter templates and it allows exactly this behavior if you define a subroutine as

sub myprint(@) { ... }

Anything similar in PHP?

print is not a variable functions

Because this is a language construct and not a function, it cannot be called using variable functions

And :

Variable functions

PHP supports the concept of variable functions. This means that if a variable name has parentheses appended to it, PHP will look for a function with the same name as whatever the variable evaluates to, and will attempt to execute it. Among other things, this can be used to implement callbacks, function tables, and so forth.