使用字符串调用 PHP 语言构造
我想缓冲一些内容。获取内容的方式取决于,这就是为什么我向缓冲区函数添加了一个类型参数来定义是包含还是回显源。
PHP
<?php
function bufferContent($source, $type = 'include') {
ob_start();
$type($source);
return ob_get_clean();
}
echo bufferContent('<html>test</html>', 'echo');
?>
输出
Fatal error: Call to undefined function echo() in #### on line 5
这是为什么?是否可以通过字符串变量调用标准 PHP 函数,例如 echo() 或 include() ?
编辑:稍微更改了问题以使其更适合答案。
I wanna buffer some content. The way how the content is fetched depends, that's why I added a type parameter to my buffer function to define whether to include or to echo the source.
PHP
<?php
function bufferContent($source, $type = 'include') {
ob_start();
$type($source);
return ob_get_clean();
}
echo bufferContent('<html>test</html>', 'echo');
?>
Output
Fatal error: Call to undefined function echo() in #### on line 5
Why's that? Isn't it possible to call a standard PHP function like echo() or include() by a string variable?
Edit: Changed question slightly to make it more suitable to the answers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
echo
不是一个函数:它是一种语言构造——因此,不能以这种方式调用它。您可能会定义一个函数,该函数本身会调用 echo - 并在调用 bufferContent 时使用您的函数:
A a reference, quoting [the manual page of `echo`][1] :
echo
is not a function : it is a language construct -- and, as such, it cannot be called this way.A possibility for you would be to define a function, that would itself call echo -- and use your function when calling
bufferContent
:A a reference, quoting [the manual page of `echo`][1] :
您不能从字符串变量中调用 echo、include、require_once、isset、empty,因为它们的行为与普通函数不同。
您可以使用
AND
您可以创建一个包装函数并调用它们,例如 :
and do
You cannot call echo, include, require_once, isset, empty from string variable because they do not behave like normal functions.
You can use
AND
You can make a wrapper function and call them instead like :
and do
会因此而受到责备,但在您的情况下,懒惰的解决方法是:
这适用于正常的函数和语言结构。尽管您确实应该在特殊情况下使用 switch,并在其他情况下保留正常的变量函数调用。
Gonna get scolded for that, but the lazy workaround in your case would be:
That works with normal functions and language constructs. Though you really ought to be using a switch for the special cases, and keep the normal variable function call for everything else.
^你需要即兴发挥一下。这就是你做你需要做的事情的方式!但有更好、更有效/通用的方法来做到这一点。
^ you need to improvise a bit. This is how you do what you need done! But there are better and more efficient/versatile ways to do it.
您不能将
echo
作为函数调用,因为它实际上不是函数,而是 PHP 中的语言构造。为了有效地调用
echo
,您可以创建一个包装方法,例如:关于通过字符串调用函数的主题,我将使用
call_user_func
所以在你的情况下
我会选择
call_user_func
优于变量函数,因为它更具可读性且不易混淆。如果我正在阅读您的代码,如果您调用call_user_func
,我会立即看到您在做什么You cannot call
echo
as a function because it is infact not a function but rather a language construct within PHP.In order to effectivly call
echo
, you can create a wrapper method such as:On the topic of calling functions via strings I'd use
call_user_func
so in your case it'd be
I'd choose
call_user_func
over a variable function because it is more readable and less confusing. If I were reading your code, I'd immediately see what you were doing if you calledcall_user_func