]> granicus.if.org Git - php/commitdiff
Fix self::class inside constant in global scope
authorNikita Popov <nikita.ppv@gmail.com>
Fri, 4 Jan 2019 08:52:04 +0000 (09:52 +0100)
committerNikita Popov <nikita.ppv@gmail.com>
Fri, 4 Jan 2019 08:52:04 +0000 (09:52 +0100)
Previously this triggered an assertion failure. The behavior is
not quite correct, in that self::class should generate an exception
if there is no self, but returns an empty string here. Fixing that
would be a bit too intrusive for the 7.2 branch.

Zend/tests/self_class_const_in_unknown_scope.phpt [new file with mode: 0644]
Zend/zend_compile.c

diff --git a/Zend/tests/self_class_const_in_unknown_scope.phpt b/Zend/tests/self_class_const_in_unknown_scope.phpt
new file mode 100644 (file)
index 0000000..240644d
--- /dev/null
@@ -0,0 +1,23 @@
+--TEST--
+Use of self::class inside a constant in an unknown scope
+--FILE--
+<?php
+
+class Test {
+    public function foobar() {
+        eval("
+            const FOO = self::class;
+            var_dump(FOO);
+        ");
+    }
+}
+(new Test)->foobar();
+
+// This should error, but doesn't
+const BAR = self::class;
+var_dump(BAR);
+
+?>
+--EXPECT--
+string(4) "Test"
+string(0) ""
index 23c3b999680c0a595640c11a247e430759017af9..d91b656182f4a4738a8835b7cafdd8ac737d2d60 100644 (file)
@@ -1513,7 +1513,7 @@ static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_a
 
        switch (fetch_type) {
                case ZEND_FETCH_CLASS_SELF:
-                       if (constant || (CG(active_class_entry) && zend_is_scope_known())) {
+                       if (CG(active_class_entry) && zend_is_scope_known()) {
                                ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
                        } else {
                                ZVAL_NULL(zv);
@@ -8009,9 +8009,7 @@ void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
        zend_ast *ast = *ast_ptr;
 
        /* Other cases already resolved by constant folding */
-       ZEND_ASSERT(ast->attr == T_CLASS_C &&
-                   CG(active_class_entry) &&
-                   (CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
+       ZEND_ASSERT(ast->attr == T_CLASS_C);
 
        {
                zval const_zv;