Skip to content

Commit 6255308

Browse files
committed
Report false for inherited private methods in method_exists()
These shadow methods only exist as internal implementation markers. This mirrors the behavior of property_exists().
1 parent fd7309d commit 6255308

File tree

4 files changed

+17
-15
lines changed

4 files changed

+17
-15
lines changed

Zend/tests/bug50810.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ var_dump($example->propertyBarExists());
4040

4141
?>
4242
--EXPECT--
43-
bool(true)
43+
bool(false)
4444
bool(true)
4545
bool(true)
4646
bool(true)

Zend/tests/method_exists_002.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ bool(true)
6262
bool(true)
6363
----
6464
bool(true)
65-
bool(true)
65+
bool(false)
6666
bool(true)
6767
----
6868
bool(true)

Zend/zend_builtin_functions.c

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,7 +1350,8 @@ ZEND_FUNCTION(method_exists)
13501350
zval *klass;
13511351
zend_string *method_name;
13521352
zend_string *lcname;
1353-
zend_class_entry * ce;
1353+
zend_class_entry *ce;
1354+
zend_function *func;
13541355

13551356
ZEND_PARSE_PARAMETERS_START(2, 2)
13561357
Z_PARAM_ZVAL(klass)
@@ -1368,28 +1369,29 @@ ZEND_FUNCTION(method_exists)
13681369
}
13691370

13701371
lcname = zend_string_tolower(method_name);
1371-
if (zend_hash_exists(&ce->function_table, lcname)) {
1372-
zend_string_release_ex(lcname, 0);
1373-
RETURN_TRUE;
1374-
} else if (Z_TYPE_P(klass) == IS_OBJECT) {
1372+
func = zend_hash_find_ptr(&ce->function_table, lcname);
1373+
zend_string_release_ex(lcname, 0);
1374+
1375+
if (func) {
1376+
RETURN_BOOL(!(func->common.fn_flags & ZEND_ACC_PRIVATE) || func->common.scope == ce);
1377+
}
1378+
1379+
if (Z_TYPE_P(klass) == IS_OBJECT) {
13751380
zend_object *obj = Z_OBJ_P(klass);
1376-
zend_function *func = Z_OBJ_HT_P(klass)->get_method(&obj, method_name, NULL);
1381+
func = Z_OBJ_HT_P(klass)->get_method(&obj, method_name, NULL);
13771382
if (func != NULL) {
13781383
if (func->common.fn_flags & ZEND_ACC_CALL_VIA_TRAMPOLINE) {
13791384
/* Returns true to the fake Closure's __invoke */
13801385
RETVAL_BOOL(func->common.scope == zend_ce_closure
13811386
&& zend_string_equals_literal(method_name, ZEND_INVOKE_FUNC_NAME));
13821387

1383-
zend_string_release_ex(lcname, 0);
13841388
zend_string_release_ex(func->common.function_name, 0);
13851389
zend_free_trampoline(func);
13861390
return;
13871391
}
1388-
zend_string_release_ex(lcname, 0);
13891392
RETURN_TRUE;
13901393
}
13911394
}
1392-
zend_string_release_ex(lcname, 0);
13931395
RETURN_FALSE;
13941396
}
13951397
/* }}} */

ext/standard/tests/class_object/method_exists_basic_001.phpt

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,10 +53,10 @@ echo "Done";
5353
---(Using string class name)---
5454
Does C::inherit_pub exist? bool(true)
5555
Does C::inherit_prot exist? bool(true)
56-
Does C::inherit_priv exist? bool(true)
56+
Does C::inherit_priv exist? bool(false)
5757
Does C::inherit_static_pub exist? bool(true)
5858
Does C::inherit_static_prot exist? bool(true)
59-
Does C::inherit_static_priv exist? bool(true)
59+
Does C::inherit_static_priv exist? bool(false)
6060
Does C::pub exist? bool(true)
6161
Does C::prot exist? bool(true)
6262
Does C::priv exist? bool(true)
@@ -68,10 +68,10 @@ Does C::non_existent exist? bool(false)
6868
---(Using object)---
6969
Does C::inherit_pub exist? bool(true)
7070
Does C::inherit_prot exist? bool(true)
71-
Does C::inherit_priv exist? bool(true)
71+
Does C::inherit_priv exist? bool(false)
7272
Does C::inherit_static_pub exist? bool(true)
7373
Does C::inherit_static_prot exist? bool(true)
74-
Does C::inherit_static_priv exist? bool(true)
74+
Does C::inherit_static_priv exist? bool(false)
7575
Does C::pub exist? bool(true)
7676
Does C::prot exist? bool(true)
7777
Does C::priv exist? bool(true)

0 commit comments

Comments
 (0)