Skip to content

Commit 41af1e6

Browse files
committed
Fix self::class inside constant in global scope
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.
1 parent cb009b1 commit 41af1e6

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
--TEST--
2+
Use of self::class inside a constant in an unknown scope
3+
--FILE--
4+
<?php
5+
6+
class Test {
7+
public function foobar() {
8+
eval("
9+
const FOO = self::class;
10+
var_dump(FOO);
11+
");
12+
}
13+
}
14+
(new Test)->foobar();
15+
16+
// This should error, but doesn't
17+
const BAR = self::class;
18+
var_dump(BAR);
19+
20+
?>
21+
--EXPECT--
22+
string(4) "Test"
23+
string(0) ""

Zend/zend_compile.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1513,7 +1513,7 @@ static zend_bool zend_try_compile_const_expr_resolve_class_name(zval *zv, zend_a
15131513

15141514
switch (fetch_type) {
15151515
case ZEND_FETCH_CLASS_SELF:
1516-
if (constant || (CG(active_class_entry) && zend_is_scope_known())) {
1516+
if (CG(active_class_entry) && zend_is_scope_known()) {
15171517
ZVAL_STR_COPY(zv, CG(active_class_entry)->name);
15181518
} else {
15191519
ZVAL_NULL(zv);
@@ -8009,9 +8009,7 @@ void zend_compile_const_expr_magic_const(zend_ast **ast_ptr) /* {{{ */
80098009
zend_ast *ast = *ast_ptr;
80108010

80118011
/* Other cases already resolved by constant folding */
8012-
ZEND_ASSERT(ast->attr == T_CLASS_C &&
8013-
CG(active_class_entry) &&
8014-
(CG(active_class_entry)->ce_flags & ZEND_ACC_TRAIT) != 0);
8012+
ZEND_ASSERT(ast->attr == T_CLASS_C);
80158013

80168014
{
80178015
zval const_zv;

0 commit comments

Comments
 (0)