Skip to content

Commit a8f0c66

Browse files
[RFC] Deprecate ReflectionClass::getConstant() for missing constants
https://wiki.php.net/rfc/deprecations_php_8_5
1 parent 2b5d978 commit a8f0c66

File tree

6 files changed

+44
-5
lines changed

6 files changed

+44
-5
lines changed

ext/reflection/php_reflection.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4855,6 +4855,11 @@ ZEND_METHOD(ReflectionClass, getConstant)
48554855
}
48564856
} ZEND_HASH_FOREACH_END();
48574857
if ((c = zend_hash_find_ptr(constants_table, name)) == NULL) {
4858+
zend_error(
4859+
E_DEPRECATED,
4860+
"ReflectionClass::getConstant() for a non-existent constant is deprecated, "
4861+
"use ReflectionClass::hasConstant() to check if the constant exists"
4862+
);
48584863
RETURN_FALSE;
48594864
}
48604865
ZVAL_COPY_OR_DUP(return_value, &c->value);

ext/reflection/tests/022.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ $class = new ReflectionClass("Foo");
99
var_dump($class->getConstant("c1"));
1010
var_dump($class->getConstant("c2"));
1111
?>
12-
--EXPECT--
12+
--EXPECTF--
1313
int(1)
14+
15+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
1416
bool(false)

ext/reflection/tests/ReflectionClass_getConstant_basic.phpt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,31 @@ foreach($classes as $class) {
2323
var_dump($rc->getConstant('doesnotexist'));
2424
}
2525
?>
26-
--EXPECT--
26+
--EXPECTF--
2727
Reflecting on class C:
2828
string(12) "hello from C"
29+
30+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
2931
bool(false)
3032
Reflecting on class D:
3133
string(12) "hello from C"
34+
35+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
3236
bool(false)
3337
Reflecting on class E:
3438
string(12) "hello from C"
39+
40+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
3541
bool(false)
3642
Reflecting on class F:
3743
string(12) "hello from F"
44+
45+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
3846
bool(false)
3947
Reflecting on class X:
48+
49+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
4050
bool(false)
51+
52+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
4153
bool(false)

ext/reflection/tests/ReflectionClass_getConstant_error.phpt

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,14 @@ var_dump($rc->getConstant(1));
1212
var_dump($rc->getConstant(1.5));
1313
var_dump($rc->getConstant(true));
1414
?>
15-
--EXPECT--
15+
--EXPECTF--
1616
Check invalid params:
17+
18+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
1719
bool(false)
20+
21+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
1822
bool(false)
23+
24+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
1925
bool(false)

ext/reflection/tests/ReflectionObject_getConstant_basic.phpt

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,31 @@ foreach($classes as $class) {
2323
var_dump($rc->getConstant('doesNotexist'));
2424
}
2525
?>
26-
--EXPECT--
26+
--EXPECTF--
2727
Reflecting on instance of class C:
2828
string(12) "hello from C"
29+
30+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
2931
bool(false)
3032
Reflecting on instance of class D:
3133
string(12) "hello from C"
34+
35+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
3236
bool(false)
3337
Reflecting on instance of class E:
3438
string(12) "hello from C"
39+
40+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
3541
bool(false)
3642
Reflecting on instance of class F:
3743
string(12) "hello from F"
44+
45+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
3846
bool(false)
3947
Reflecting on instance of class X:
48+
49+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
4050
bool(false)
51+
52+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
4153
bool(false)

ext/reflection/tests/bug38653.phpt

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,11 @@ var_dump($foo->getConstant("no such const"));
2020

2121
echo "Done\n";
2222
?>
23-
--EXPECT--
23+
--EXPECTF--
2424
int(10)
2525
string(0) ""
2626
string(4) "test"
27+
28+
Deprecated: ReflectionClass::getConstant() for a non-existent constant is deprecated, use ReflectionClass::hasConstant() to check if the constant exists in %s on line %d
2729
bool(false)
2830
Done

0 commit comments

Comments
 (0)