Skip to content

直接对表达式判空编译成isset会报错 #126

@shirookie

Description

@shirookie

问题描述

如果源码里这样写:

a() !== undefined;

编译出来的结果是:

isset(a());

运行时就会报错,如下

Fatal error: Cannot use isset() on the result of an expression (you can use "null !== expression" instead) in /path/to/file.php

临时方案

通过改源码,有两种方式可解

方法1:反过来判断

源码:

undefined !== a();

编译结果:

null !== a();

方法2:先赋值给变量,再判断

源码:

const b = a();
return b !== undefined;

编译结果:

$b = a();
return isset($b);

期望

直接编译成:新建一个临时变量,对临时变量执行 isset

大概就像 babel 对剪头函数进行转换的时候,在函数最外面先建个变量指向当前 this 那种

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions