php - 编译错误 : Cannot use isset() on the result of an expression

标签 php symfony twig symfony-2.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

我不知道发生了什么问题或如何解决这个问题,所以我需要一些建议。这是报告 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 " ";

我有这个 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';
    }
}

我在模板中使用如下:

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

这里的问题在哪里?有什么建议吗?

最佳答案

来自 documentation :

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

您没有直接将变量传递给 isset()。所以需要先计算出值,赋给一个变量,然后传递给isset()

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

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

你需要做的是:

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

关于php - 编译错误 : Cannot use isset() on the result of an expression,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29636880/

相关文章:

logging - Symfony2 日志中由 "OPTIONS"http 请求引起的 404 错误

php - Blade 和 Twig 之间的区别

php - 限制 Apigility 中的结果

php - 如何使用php获取存储在Mysql中的多张图片?

php - 将 JSON 转换为多维 JavaScript 数组

php - 插入 DB 后小数点四舍五入

php - 学说 : Default value on joinColumn field

php - 安装错误: Overwrite Akeneo ProductController

php - 使用 PHPed 调试时无法找到模板 AcmeDemoBundle :Demo:hello. html.twig Symfony 2

symfony - Twig 有什么方法可以确定方法/参数是否存在而不用到处放置 if 吗?