使用字符串调用 PHP 语言构造

发布于 2024-10-31 08:57:18 字数 535 浏览 4 评论 0原文

我想缓冲一些内容。获取内容的方式取决于,这就是为什么我向缓冲区函数添加了一个类型参数来定义是包含还是回显源。

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

别想她 2024-11-07 08:57:18

echo 不是一个函数:它是一种语言构造——因此,不能以这种方式调用它。

您可能会定义一个函数,该函数本身会调用 echo - 并在调用 bufferContent 时使用您的函数:

function my_echo($str) {
    echo $str;
}
echo bufferContent('<html>test</html>', 'my_echo');

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 :

function my_echo($str) {
    echo $str;
}
echo bufferContent('<html>test</html>', 'my_echo');

A a reference, quoting [the manual page of `echo`][1] :

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

岁月无声 2024-11-07 08:57:18

您不能从字符串变量中调用 echo、include、require_once、isset、empty,因为它们的行为与普通函数不同。
您可以使用

include "file.php";

AND

include("file.php");

您可以创建一个包装函数并调用它们,例如 :

function wrap_echo($str) { echo($str); };

and do

$f = "wrap_echo";
$f("sth");

You cannot call echo, include, require_once, isset, empty from string variable because they do not behave like normal functions.
You can use

include "file.php";

AND

include("file.php");

You can make a wrapper function and call them instead like :

function wrap_echo($str) { echo($str); };

and do

$f = "wrap_echo";
$f("sth");
凯凯我们等你回来 2024-11-07 08:57:18

会因此而受到责备,但在您的情况下,懒惰的解决方法是:

eval(" $type(\$source); ");

这适用于正常的函数和语言结构。尽管您确实应该在特殊情况下使用 switch,并在其他情况下保留正常的变量函数调用。

Gonna get scolded for that, but the lazy workaround in your case would be:

eval(" $type(\$source); ");

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.

中二柚 2024-11-07 08:57:18
function buffer_content($source, $type = 'echo') {
    if(!is_string($type)){
        trigger_error('$type must be a string.', E_USER_WARNING);
        return false;
    }
    if(is_object($source) and method_exists($source, '__toString')){
        $source = strval($source);
    }elseif(is_scalar($source)){
        $source = strval($source);
    }elseif(!is_string($source)){
        trigger_error('$source must be a string as non-scalars do not echo nicely.', E_USER_WARNING);
        return false;
    }
    ob_start();
    switch(strtolower($type)){
        case 'include': include $source; break;
        case 'include_once': include_once $source; break;
        case 'require': require $source; break;
        case 'require_once': require_once $source; break;
        case 'echo': echo $source; break;
        default: trigger_error("\$type '{$type}' is not supported.", E_USER_WARNING); break;
    }
    return ob_get_clean();
}

^你需要即兴发挥一下。这就是你做你需要做的事情的方式!但有更好、更有效/通用的方法来做到这一点。

function buffer_content($source, $type = 'echo') {
    if(!is_string($type)){
        trigger_error('$type must be a string.', E_USER_WARNING);
        return false;
    }
    if(is_object($source) and method_exists($source, '__toString')){
        $source = strval($source);
    }elseif(is_scalar($source)){
        $source = strval($source);
    }elseif(!is_string($source)){
        trigger_error('$source must be a string as non-scalars do not echo nicely.', E_USER_WARNING);
        return false;
    }
    ob_start();
    switch(strtolower($type)){
        case 'include': include $source; break;
        case 'include_once': include_once $source; break;
        case 'require': require $source; break;
        case 'require_once': require_once $source; break;
        case 'echo': echo $source; break;
        default: trigger_error("\$type '{$type}' is not supported.", E_USER_WARNING); break;
    }
    return ob_get_clean();
}

^ 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.

少跟Wǒ拽 2024-11-07 08:57:18

您不能将 echo 作为函数调用,因为它实际上不是函数,而是 PHP 中的语言构造。

为了有效地调用 echo,您可以创建一个包装方法,例如:

function call_echo($str){
  echo $str;
}

关于通过字符串调用函数的主题,我将使用 call_user_func

mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )

所以在你的情况下

call_user_func($type, $source);

我会选择 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:

function call_echo($str){
  echo $str;
}

On the topic of calling functions via strings I'd use call_user_func

mixed call_user_func ( callback $function [, mixed $parameter [, mixed $... ]] )

so in your case it'd be

call_user_func($type, $source);

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 called call_user_func

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文