From 6497c6c455ef0cb2debe1891d6c6d00ece95a3c2 Mon Sep 17 00:00:00 2001 From: Ilija Tovilo Date: Wed, 20 Aug 2025 23:20:47 +0200 Subject: [PATCH] Don't substitute self/parent with anonymous class Fixes GH-18373 Closes GH-19537 --- NEWS | 2 ++ Zend/tests/gh19304.phpt | 2 +- Zend/zend_compile.c | 8 ++++++-- ext/reflection/tests/gh18373.phpt | 14 ++++++++++++++ 4 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 ext/reflection/tests/gh18373.phpt diff --git a/NEWS b/NEWS index 6c2db856000..3bcc968bab6 100644 --- a/NEWS +++ b/NEWS @@ -25,6 +25,8 @@ PHP NEWS operators" RFC, meaning that incrementing non-numeric strings is now deprecated. (Girgias). . Various closure binding issues are now deprecated. (alexandre-daubois) + . Fixed bug GH-18373 (Don't substitute self/parent with anonymous class). + (ilutov) - Filter: . Added support for configuring the URI parser for FILTER_VALIDATE_URL diff --git a/Zend/tests/gh19304.phpt b/Zend/tests/gh19304.phpt index c77fc2d6fac..47e20af6462 100644 --- a/Zend/tests/gh19304.phpt +++ b/Zend/tests/gh19304.phpt @@ -15,4 +15,4 @@ try { ?> --EXPECT-- -Cannot assign int to property class@anonymous::$v of type class@anonymous +Cannot assign int to property class@anonymous::$v of type self diff --git a/Zend/zend_compile.c b/Zend/zend_compile.c index 205023fa69b..503b4699742 100644 --- a/Zend/zend_compile.c +++ b/Zend/zend_compile.c @@ -7091,16 +7091,20 @@ static zend_type zend_compile_single_typename(zend_ast *ast) ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_SELF || fetch_type == ZEND_FETCH_CLASS_PARENT); zend_ensure_valid_class_fetch_type(fetch_type); + + bool substitute_self_parent = zend_is_scope_known() + && !(CG(active_class_entry)->ce_flags & ZEND_ACC_ANON_CLASS); + if (fetch_type == ZEND_FETCH_CLASS_SELF) { /* Scope might be unknown for unbound closures and traits */ - if (zend_is_scope_known()) { + if (substitute_self_parent) { class_name = CG(active_class_entry)->name; ZEND_ASSERT(class_name && "must know class name when resolving self type at compile time"); } } else { ZEND_ASSERT(fetch_type == ZEND_FETCH_CLASS_PARENT); /* Scope might be unknown for unbound closures and traits */ - if (zend_is_scope_known()) { + if (substitute_self_parent) { class_name = CG(active_class_entry)->parent_name; ZEND_ASSERT(class_name && "must know class name when resolving parent type at compile time"); } diff --git a/ext/reflection/tests/gh18373.phpt b/ext/reflection/tests/gh18373.phpt new file mode 100644 index 00000000000..30aa1a0f5fe --- /dev/null +++ b/ext/reflection/tests/gh18373.phpt @@ -0,0 +1,14 @@ +--TEST-- +GH-18373: Don't substitute self/parent with anonymous class +--FILE-- +getMethod('test')->getReturnType()->getName(), "\n"; + +?> +--EXPECT-- +self