Skip to content

Commit d3f4ead

Browse files
[RFC] Deprecate ReflectionMethod invocation for static methods with objects
`ReflectionMethod::invoke()` and `::invokeArgs()` are affected, but share implementation code and so are updated together. https://wiki.php.net/rfc/deprecations_php_8_6
1 parent 1b5de53 commit d3f4ead

5 files changed

Lines changed: 26 additions & 3 deletions

File tree

‎UPGRADING‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,9 @@ PHP 8.6 UPGRADE NOTES
483483
. Calling ReflectionProperty::setRawValue() with an object that is not an
484484
instance of the class on which the property was declared is now deprecated.
485485
RFC: https://wiki.php.net/rfc/deprecations_php_8_6#deprecate_reflectionpropertysetvalue_and_reflectionpropertysetrawvalue_with_wrong_types
486+
. Calling ReflectionMethod::invoke() or ReflectionMethod::invokeArgs() with
487+
an object and a static method is now deprecated.
488+
RFC: https://wiki.php.net/rfc/deprecations_php_8_6#deprecate_reflectionmethodinvoke_and_reflectionmethodinvokeargs_with_objects_for_static_methods
486489

487490
- SPL:
488491
. The spl_classes() function is now deprecated, use

‎ext/reflection/php_reflection.c‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3375,7 +3375,21 @@ static void reflection_method_invoke(INTERNAL_FUNCTION_PARAMETERS, bool variadic
33753375
* Else, we verify that the given object is an instance of the class.
33763376
*/
33773377
if (mptr->common.fn_flags & ZEND_ACC_STATIC) {
3378-
object = NULL;
3378+
if (object) {
3379+
zend_string *method_name = get_active_function_or_method_name();
3380+
zend_error(
3381+
E_DEPRECATED,
3382+
"Calling %pS() for static method %pS::%pS() does not need an object parameter",
3383+
method_name,
3384+
mptr->common.scope->name,
3385+
mptr->common.function_name
3386+
);
3387+
zend_string_release(method_name);
3388+
if (UNEXPECTED(EG(exception))) {
3389+
RETURN_THROWS();
3390+
}
3391+
object = NULL;
3392+
}
33793393
obj_ce = mptr->common.scope;
33803394
} else {
33813395
if (!object) {

‎ext/reflection/tests/ReflectionMethod_invokeArgs_error3.phpt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ try {
6565
}
6666

6767
?>
68-
--EXPECT--
68+
--EXPECTF--
6969
Non-instance:
7070
string(72) "Given object is not an instance of the class this method was declared in"
7171

@@ -75,6 +75,8 @@ Exception: Using $this when not in object context
7575
NULL
7676

7777
Private method:
78+
79+
Deprecated: Calling ReflectionMethod::invokeArgs() for static method TestClass::privateMethod() does not need an object parameter in %s on line %d
7880
Called privateMethod()
7981
NULL
8082

‎ext/reflection/tests/ReflectionMethod_invoke_basic.phpt‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ NULL
104104
Static method:
105105
ReflectionMethod::invoke() expects at least 1 argument, 0 given
106106
ReflectionMethod::invoke(): Argument #1 ($object) must be of type ?object, true given
107+
108+
Deprecated: Calling ReflectionMethod::invoke() for static method TestClass::staticMethod() does not need an object parameter in %s on line %d
107109
Called staticMethod()
108110
Exception: Using $this when not in object context
109111
NULL

‎ext/reflection/tests/ReflectionMethod_invoke_error1.phpt‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,16 @@ try {
5353
}
5454

5555
?>
56-
--EXPECT--
56+
--EXPECTF--
5757
invoke() on a non-object:
5858
string(85) "ReflectionMethod::invoke(): Argument #1 ($object) must be of type ?object, true given"
5959

6060
invoke() on a non-instance:
6161
string(72) "Given object is not an instance of the class this method was declared in"
6262

6363
Private method:
64+
65+
Deprecated: Calling ReflectionMethod::invoke() for static method TestClass::privateMethod() does not need an object parameter in %s on line %d
6466
Called privateMethod()
6567
NULL
6668

0 commit comments

Comments
 (0)