Skip to content

Commit f094f41

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 602a4ac commit f094f41

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

UPGRADING

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

555557
- XML:
556558
. The xml_set_object() function has been deprecated.

Zend/tests/bug18556.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ echo "\n";
2323
setlocale(LC_ALL, "tr_TR.utf8");
2424
foreach(get_declared_classes() as $class)
2525
{
26-
if(!class_exists($class))
26+
if(!enum_exists($class) && !class_exists($class))
2727
echo "$class No Longer Exists!\n";
2828

2929
}
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
@@ -1062,6 +1062,9 @@ static inline void _class_exists_impl(zval *return_value, zend_string *name, boo
10621062
if (ZSTR_HAS_CE_CACHE(name)) {
10631063
ce = ZSTR_GET_CE_CACHE(name);
10641064
if (ce) {
1065+
if (flags == ZEND_ACC_LINKED && ce->ce_flags & ZEND_ACC_ENUM) {
1066+
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));
1067+
}
10651068
RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
10661069
}
10671070
}
@@ -1082,6 +1085,9 @@ static inline void _class_exists_impl(zval *return_value, zend_string *name, boo
10821085
}
10831086

10841087
if (ce) {
1088+
if (flags == ZEND_ACC_LINKED && ce->ce_flags & ZEND_ACC_ENUM) {
1089+
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));
1090+
}
10851091
RETURN_BOOL(((ce->ce_flags & flags) == flags) && !(ce->ce_flags & skip_flags));
10861092
} else {
10871093
RETURN_FALSE;

ext/standard/tests/class_object/get_declared_classes_basic_001.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ echo "\n-- Testing get_declared_classes() function with Zero arguments --\n";
99
var_dump(get_declared_classes());
1010

1111
foreach (get_declared_classes() as $class) {
12-
if (!class_exists($class)) {
12+
if (!enum_exists($class) && !class_exists($class)) {
1313
echo "Error: $class is not a valid class.\n";
1414
}
1515
}

0 commit comments

Comments
 (0)