php 编译错误:不能对表达式的结果使用 isset()

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29636880/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 00:29:49  来源:igfitidea点击:

Compile Error: Cannot use isset() on the result of an expression

phpsymfonytwigsymfony-2.7

提问by ReynierPM

I'm getting this error in a app I am migrating from SF2.0.x to SF2.7:

我在从 SF2.0.x 迁移到 SF2.7 的应用程序中收到此错误:

[1] Symfony\Component\Debug\Exception\FatalErrorException: Compile Error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead)
    at n/a
        in /var/www/html/reptooln_admin/app/cache/dev/twig/68/7f/63589dd3687cb849dd68e6b6c10aa99eda1d82f95a5f3ac52a864d200499.php line 39

I don't know what is failing or how to fix this so I need some advise. This is the line at cache file where the Stacktrace is reported:

我不知道出了什么问题或如何解决这个问题,所以我需要一些建议。这是报告 Stacktrace 的缓存文件中的行:

    if ((((empty((isset($context["form_action"]) ? $context["form_action"] : $this->getContext($context, "form_action"))) == true) || (isnull((isset($context["form_action"]) ? $context["form_action"] : $this->getContext($context, "form_action"))) == true)) || (isset((isset($context["form_action"]) ? $context["form_action"] : $this->getContext($context, "form_action"))) == false))) {
                echo " ";
                $context["form_action"] = "";
                echo " ";

What I have this TwigExtension:

我有这个 TwigExtension:

class PDOneTwigExtension extends \Twig_Extension
{
    public function getFilters()
    {
        return array(
            'var_dump'   => new \Twig_Filter_Function('var_dump'),
            'empty' => new \Twig_Filter_Function('empty', array($this, 'is_empty')),
            'isset' => new \Twig_Filter_Function('isset', array($this, 'is_set')),
            'isnull' => new \Twig_Filter_Function('isnull', array($this, 'is_null')),
            'ucfirst' => new \Twig_Filter_Function('ucfirst', array($this, 'uc_first')),
            'ucwords' => new \Twig_Filter_Function('ucwords', array($this, 'uc_words')),
            'count' => new \Twig_Filter_Function('count', array($this, 'co_unt')),
            'sizeof' => new \Twig_Filter_Function('sizeof', array($this, 'size_of')),
            'concat' => new \Twig_Filter_Function('concat', array($this, 'concat')),
            'in_array' => new \Twig_Filter_Function('in_array', array($this, 'inarray')),
            'array' => new \Twig_Filter_Function('array', array($this, 'array_')),
            'add_to_array' => new \Twig_Filter_Function('add_to_array', array($this, 'add_to_array')),
            'replace' => new \Twig_Filter_Function('replace', array($this, 'replace')),
            'htmlentitydecode' => new \Twig_Filter_Function('htmlentitydecode', array($this, 'htmlentitydecode'))
        );
    }

    public function is_empty($sentence)
    {
        return empty($sentence) ? true : false;
    }

    // rest of methods goes here

    public function getName()
    {
        return 'pdone_twig_extension';
    }
}

And I'm using at template as follow:

我在模板中使用如下:

{% if form_action|empty == true or form_action|isnull == true or form_action|isset == false %} {% set form_action = '' %} {% endif %}

Where could be the issue here? Any advice?

这里可能是哪里出了问题?有什么建议吗?

回答by Dan Blows

From documentation:

文档

isset() only works with variables as passing anything else will result in a parse error.

isset() 仅适用于变量,因为传递任何其他内容都会导致解析错误。

You're not directly passing a variable to isset(). So you need to calculate the value first, assign it to a variable, and then pass that to isset().

您没有直接将变量传递给isset(). 因此,您需要先计算该值,将其分配给一个变量,然后将其传递给isset().

For example, what you're doing at the moment is something like:

例如,你现在正在做的事情是这样的:

if(isset($something === false)) { } // throws a parse error, because $something === false is not a variable

What you need to do instead is:

你需要做的是:

$something = false;
if(isset($something)) { ... }

回答by Alex

Why are you doing so much useless code?

你为什么要写这么多无用的代码?

Why don't just:

为什么不只是:

{% if form_action|empty == true %} {% set form_action = '' %} {% endif %}