Skip to content

Commit 743c378

Browse files
committed
show a more informative error when accessing a private class
1 parent a3a2da1 commit 743c378

File tree

6 files changed

+25
-4
lines changed

6 files changed

+25
-4
lines changed

Zend/zend_compile.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9171,7 +9171,7 @@ static void zend_compile_class_decl(znode *result, zend_ast *ast, bool toplevel)
91719171
// - public
91729172
// - private
91739173
// - protected
9174-
int propFlags = decl->attr & (ZEND_ACC_PUBLIC|ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE);
9174+
int propFlags = (decl->attr & (ZEND_ACC_PUBLIC|ZEND_ACC_PROTECTED|ZEND_ACC_PRIVATE)) | ZEND_ACC_INNER_CLASS_REFERENCE;
91759175

91769176
// there are two things we need to inject into the nested parent:
91779177
// - a static property that contains the name of the nested class

Zend/zend_compile.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ typedef struct _zend_oparray_context {
253253
/* or IS_CONSTANT_VISITED_MARK | | | */
254254
#define ZEND_CLASS_CONST_IS_CASE (1 << 6) /* | | | X */
255255
/* | | | */
256-
/* Property Flags (unused: 13...) | | | */
256+
/* Property Flags (unused: 14...) | | | */
257257
/* =========== | | | */
258258
/* | | | */
259259
/* Promoted property / parameter | | | */
@@ -267,6 +267,9 @@ typedef struct _zend_oparray_context {
267267
#define ZEND_ACC_PROTECTED_SET (1 << 11) /* | | X | */
268268
#define ZEND_ACC_PRIVATE_SET (1 << 12) /* | | X | */
269269
/* | | | */
270+
/* Property is a reference | | X | */
271+
#define ZEND_ACC_INNER_CLASS_REFERENCE (1 << 13) /* | | X | */
272+
/* | | | */
270273
/* Class Flags (unused: 31) | | | */
271274
/* =========== | | | */
272275
/* | | | */

Zend/zend_object_handlers.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,11 @@ static zend_never_inline zend_property_info *zend_get_parent_private_property(ze
298298

299299
static ZEND_COLD zend_never_inline void zend_bad_property_access(const zend_property_info *property_info, const zend_class_entry *ce, const zend_string *member) /* {{{ */
300300
{
301-
zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
301+
if (property_info->flags & ZEND_ACC_INNER_CLASS_REFERENCE) {
302+
zend_throw_error(NULL, "Cannot access %s inner class %s::%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
303+
} else {
304+
zend_throw_error(NULL, "Cannot access %s property %s::$%s", zend_visibility_string(property_info->flags), ZSTR_VAL(ce->name), ZSTR_VAL(member));
305+
}
302306
}
303307
/* }}} */
304308

Zend/zend_vm_def.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9825,6 +9825,13 @@ ZEND_VM_HANDLER(209, ZEND_INIT_PARENT_PROPERTY_HOOK_CALL, CONST, UNUSED|NUM, NUM
98259825
HANDLE_EXCEPTION();
98269826
}
98279827
if (prop_info->flags & ZEND_ACC_PRIVATE) {
9828+
// break here
9829+
if (prop_info->flags & ZEND_ACC_INNER_CLASS_REFERENCE) {
9830+
zend_throw_error(NULL, "Cannot access private inner class %s::%s", ZSTR_VAL(parent_ce->name), ZSTR_VAL(property_name));
9831+
UNDEF_RESULT();
9832+
HANDLE_EXCEPTION();
9833+
}
9834+
98289835
zend_throw_error(NULL, "Cannot access private property %s::$%s", ZSTR_VAL(parent_ce->name), ZSTR_VAL(property_name));
98299836
UNDEF_RESULT();
98309837
HANDLE_EXCEPTION();

Zend/zend_vm_execute.h

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/classes/inner_class_003.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ new Foo::Bar();
2121
--EXPECTF--
2222
Inner
2323

24-
Fatal error: Uncaught Error: Cannot access private property Foo::$Bar in %s:%d
24+
Fatal error: Uncaught Error: Cannot access private inner class Foo::Bar in %s:%d
2525
Stack trace:
2626
#0 {main}
2727
thrown in %s on line %d

0 commit comments

Comments
 (0)