Skip to content

Commit 033d9b8

Browse files
committed
ext/standard: Deprecate calling class_exists with Enums
Calling `class_exists()` with an Enum name emits a deprecation notice: ``` Deprecated: using class_exists() for enums is deprecated, triggerred for calling class_exists() for enum "Foo". Use enum_exists() instead %s on line %d ```
1 parent ac3cdf5 commit 033d9b8

File tree

3 files changed

+83
-0
lines changed

3 files changed

+83
-0
lines changed

UPGRADING

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -548,6 +548,8 @@ PHP 8.4 UPGRADE NOTES
548548
- str_getcsv()
549549
is now deprecated.
550550
RFC: https://wiki.php.net/rfc/deprecations_php_8_4#deprecate_proprietary_csv_escaping_mechanism
551+
. Calling class_exists() with an enum name is deprecated in favor of the
552+
enum_exists() function.
551553

552554
- XML:
553555
. The xml_set_object() function has been deprecated.
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
--TEST--
2+
Calling class_exists() on Enums should trigger a deprecation
3+
--DESCRIPTION--
4+
Calling class_exists() on Enums is deprecated, but it should continue to return
5+
the correct result. The underlying functionality for (class|trait|enum|interface)_exists
6+
is shared, so this deprecation should only affect class_exists() function and no
7+
other functions.
8+
--FILE--
9+
<?php
10+
11+
enum Foo {
12+
case Bar;
13+
}
14+
15+
class Bar {
16+
17+
}
18+
19+
spl_autoload_register(function ($className) {
20+
echo "Triggered autoloader with Enum $className\n";
21+
22+
if ($className === 'Quux') {
23+
enum Quux {}
24+
}
25+
});
26+
27+
echo "Testing: Foo";
28+
var_dump(class_exists('Foo'));
29+
var_dump(enum_exists('Foo'));
30+
31+
echo "Testing: Bar\n";
32+
var_dump(class_exists('Bar'));
33+
var_dump(!enum_exists('Bar'));
34+
var_dump(!enum_exists('Bar', true));
35+
36+
echo "Testing: Quux\n";
37+
var_dump(!class_exists('Quux', false));
38+
var_dump(class_exists('Quux', true));
39+
var_dump(class_exists('Quux', true));
40+
var_dump(enum_exists('Quux', true));
41+
42+
echo "trait_exists() and interface_exists()\n";
43+
var_dump(!trait_exists('Foo'));
44+
var_dump(!trait_exists('Bar'));
45+
var_dump(!trait_exists('Quux'));
46+
var_dump(!interface_exists('Foo'));
47+
var_dump(!interface_exists('Bar'));
48+
var_dump(!interface_exists('Quux'));
49+
?>
50+
--EXPECTF--
51+
Testing: Foo
52+
Deprecated: using class_exists() for enums is deprecated, triggerred for calling class_exists() for enum "Foo". Use enum_exists() instead %s on line %d
53+
bool(true)
54+
bool(true)
55+
Testing: Bar
56+
bool(true)
57+
bool(true)
58+
bool(true)
59+
Testing: Quux
60+
bool(true)
61+
Triggered autoloader with Enum Quux
62+
63+
Deprecated: using class_exists() for enums is deprecated, triggerred for calling class_exists() for enum "Quux". Use enum_exists() instead %s on line %d
64+
bool(true)
65+
66+
Deprecated: using class_exists() for enums is deprecated, triggerred for calling class_exists() for enum "Quux". Use enum_exists() instead %s on line %d
67+
bool(true)
68+
bool(true)
69+
trait_exists() and interface_exists()
70+
bool(true)
71+
bool(true)
72+
bool(true)
73+
bool(true)
74+
bool(true)
75+
bool(true)

Zend/zend_builtin_functions.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1061,6 +1061,9 @@ static inline void _class_exists_impl(zval *return_value, zend_string *name, boo
10611061
if (ZSTR_HAS_CE_CACHE(name)) {
10621062
ce = ZSTR_GET_CE_CACHE(name);
10631063
if (ce) {
1064+
if (flags == ZEND_ACC_LINKED && ce->ce_flags & ZEND_ACC_ENUM) {
1065+
zend_error(E_DEPRECATED, "using class_exists() for enums is deprecated, triggerred for calling class_exists() for enum \"%s\". Use enum_exists() instead", ZSTR_VAL(ce->name));
1066+
}
10641067
RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
10651068
}
10661069
}
@@ -1081,6 +1084,9 @@ static inline void _class_exists_impl(zval *return_value, zend_string *name, boo
10811084
}
10821085

10831086
if (ce) {
1087+
if (flags == ZEND_ACC_LINKED && ce->ce_flags & ZEND_ACC_ENUM) {
1088+
zend_error(E_DEPRECATED, "using class_exists() for enums is deprecated, triggerred for calling class_exists() for enum \"%s\". Use enum_exists() instead", ZSTR_VAL(ce->name));
1089+
}
10841090
RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
10851091
} else {
10861092
RETURN_FALSE;

0 commit comments

Comments
 (0)