我可以创建一个PHP函数,我可以在没有括号的情况下调用它吗? [英] Can I create a PHP function that I can call without parentheses?

查看:635
本文介绍了我可以创建一个PHP函数,我可以在没有括号的情况下调用它吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个实际上可以代替打印的函数,并且我希望不用括号就可以调用它,就像调用print一样。

 #替换
print $ foo,$ bar,\\\
;

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

在Perl中,您可以使用参数模板创建子例程,如果您定义子例程作为

  sub myprint(@){...} 

在PHP中有类似的东西吗?

解决方案

print不是变量函数



< blockquote>

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





变量函数



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.

这篇关于我可以创建一个PHP函数,我可以在没有括号的情况下调用它吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆