diff --git a/src/CompilerBase.php b/src/CompilerBase.php index 2d8f90dd..c0bd4720 100644 --- a/src/CompilerBase.php +++ b/src/CompilerBase.php @@ -3228,8 +3228,15 @@ class CompilerBase implements PropertyAccessContext protected function parseChainedExpr(NodeAbstract $node, string $op, bool $getValue = false): string { + $name = $this->parseIdentifier($node); + + // 如果empty/isset中的变量不存在,直接返回true/false + if (($op == self::OP_EMPTY || $op == self::OP_ISSET) && $this->isVarExpr($node) && !$this->hasVar($name)) { + return $op == self::OP_EMPTY ? 'true' : 'false'; + } + // TypePHP 编译器不允许操作未定义的变量,PHP 的 isset($var) 可能 $var 未定义 - $this->checkVarMustExist($node, $this->parseIdentifier($node)); + $this->checkVarMustExist($node, $name); $fn = $this->getChainedFunc($op); $expr = $node; if ($this->isVarExpr($expr)) { diff --git a/tests/std/core/empty_isset.phpt b/tests/std/core/empty_isset.phpt new file mode 100644 index 00000000..f795fdcb --- /dev/null +++ b/tests/std/core/empty_isset.phpt @@ -0,0 +1,16 @@ +--TEST-- +empty() / isset() undefined var +--FILE-- + +--EXPECT-- +bool(true) +bool(false) +bool(false) +bool(true)