Skip to content

Commit 6155a1f

Browse files
committed
Allow methods in parent classes to call protected methods in derived
classes
1 parent 68ffe71 commit 6155a1f

File tree

1 file changed

+22
-7
lines changed

1 file changed

+22
-7
lines changed

Zend/zend_execute.c

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2308,13 +2308,28 @@ inline int zend_check_private(zend_execute_data *execute_data, zend_class_entry
23082308

23092309
/* Ensures that we're allowed to call a protected method.
23102310
*/
2311-
inline int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope, int fn_flags)
2311+
inline int zend_check_protected(zend_class_entry *ce, zend_class_entry *scope)
23122312
{
2313-
while (ce) {
2314-
if (ce==scope) {
2313+
zend_class_entry *fbc_scope = ce;
2314+
2315+
/* Is the context that's calling the function, the same as one of
2316+
* the function's parents?
2317+
*/
2318+
while (fbc_scope) {
2319+
if (fbc_scope==scope) {
23152320
return 1;
23162321
}
2317-
ce = ce->parent;
2322+
fbc_scope = fbc_scope->parent;
2323+
}
2324+
2325+
/* Is the function's scope the same as our current object context,
2326+
* or any of the parents of our context?
2327+
*/
2328+
while (scope) {
2329+
if (scope==ce) {
2330+
return 1;
2331+
}
2332+
scope = scope->parent;
23182333
}
23192334
return 0;
23202335
}
@@ -2355,7 +2370,7 @@ int zend_init_ctor_call_handler(ZEND_OPCODE_HANDLER_ARGS)
23552370
} else if ((EX(fbc)->common.fn_flags & ZEND_ACC_PROTECTED)) {
23562371
/* Ensure that if we're calling a protected function, we're allowed to do so.
23572372
*/
2358-
if (!zend_check_protected(EG(scope), EX(fbc)->common.scope, EX(fbc)->common.fn_flags)) {
2373+
if (!zend_check_protected(EX(fbc)->common.scope, EG(scope))) {
23592374
zend_error(E_ERROR, "Call to protected constructor from context '%s'", EG(scope) ? EG(scope)->name : "");
23602375
}
23612376
}
@@ -2416,7 +2431,7 @@ int zend_init_method_call_handler(ZEND_OPCODE_HANDLER_ARGS)
24162431
} else if ((EX(fbc)->common.fn_flags & ZEND_ACC_PROTECTED)) {
24172432
/* Ensure that if we're calling a protected function, we're allowed to do so.
24182433
*/
2419-
if (!zend_check_protected(EG(scope), EX(fbc)->common.scope, EX(fbc)->common.fn_flags)) {
2434+
if (!zend_check_protected(EX(fbc)->common.scope, EG(scope))) {
24202435
zend_error(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(EX(fbc)->common.fn_flags), ZEND_FN_SCOPE_NAME(EX(fbc)), function_name_strval, EG(scope) ? EG(scope)->name : "");
24212436
}
24222437
}
@@ -2498,7 +2513,7 @@ int zend_init_static_method_call_handler(ZEND_OPCODE_HANDLER_ARGS)
24982513
} else if ((EX(fbc)->common.fn_flags & ZEND_ACC_PROTECTED)) {
24992514
/* Ensure that if we're calling a protected function, we're allowed to do so.
25002515
*/
2501-
if (!zend_check_protected(EG(scope), EX(fbc)->common.scope, EX(fbc)->common.fn_flags)) {
2516+
if (!zend_check_protected(EG(scope), EX(fbc)->common.scope)) {
25022517
zend_error(E_ERROR, "Call to %s method %s::%s() from context '%s'", zend_visibility_string(EX(fbc)->common.fn_flags), ZEND_FN_SCOPE_NAME(EX(fbc)), function_name_strval, EG(scope) ? EG(scope)->name : "");
25032518
}
25042519
}

0 commit comments

Comments
 (0)