[php-src] master: Implement reflection constant
| From: | Ilija Tovilo | Date: | Wed, 17 Apr 2024 20:54:26 +0000 |
| Subject: | [php-src] master: Implement reflection constant | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-128152@lists.php.net to get a copy of this message | ||
Author: Ilija Tovilo (iluuu1994)
Date: 2024-04-17T22:53:09+02:00
Commit: https://github.com/php/php-src/commit/e23440e5a64879ba00c8755e24b7a9ce0560fd08
Raw diff: https://github.com/php/php-src/commit/e23440e5a64879ba00c8755e24b7a9ce0560fd08.diff
Implement reflection constant
Fixes GH-13570
Closes GH-13669
Changed paths:
A ext/reflection/tests/ReflectionConstant_double_construct.phpt
A ext/reflection/tests/ReflectionConstant_dump.phpt
A ext/reflection/tests/ReflectionConstant_getName.phpt
A ext/reflection/tests/ReflectionConstant_getValue.phpt
A ext/reflection/tests/ReflectionConstant_inexistent.phpt
A ext/reflection/tests/ReflectionConstant_isDeprecated.phpt
A ext/reflection/tests/ReflectionConstant_ns.phpt
M UPGRADING
M Zend/zend_constants.c
M Zend/zend_constants.h
M ext/reflection/php_reflection.c
M ext/reflection/php_reflection.stub.php
M ext/reflection/php_reflection_arginfo.h
M ext/reflection/tests/ReflectionExtension_getClasses_basic.phpt
M sapi/cli/tests/006.phpt
Diff:
diff --git a/UPGRADING b/UPGRADING
index e2fda74fc892e..ad6e5b839a8a0 100644
--- a/UPGRADING
+++ b/UPGRADING
@@ -252,6 +252,7 @@ PHP 8.4 UPGRADE NOTES
experience.
. ReflectionClassConstant::__toString() and ReflectionProperty::__toString()
now returns the attached doc comments.
+ . ReflectionConstant was introduced.
- Standard:
. stream_bucket_make_writeable() and stream_bucket_new() will now return a
diff --git a/Zend/zend_constants.c b/Zend/zend_constants.c
index ed649ba298592..8c09f67540669 100644
--- a/Zend/zend_constants.c
+++ b/Zend/zend_constants.c
@@ -268,7 +268,7 @@ ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len)
return NULL;
}
-static zend_constant *zend_get_constant_impl(zend_string *name)
+ZEND_API zend_constant *zend_get_constant_ptr(zend_string *name)
{
zend_constant *c = zend_hash_find_ptr(EG(zend_constants), name);
if (c) {
@@ -285,7 +285,7 @@ static zend_constant *zend_get_constant_impl(zend_string *name)
ZEND_API zval *zend_get_constant(zend_string *name)
{
- zend_constant *c = zend_get_constant_impl(name);
+ zend_constant *c = zend_get_constant_ptr(name);
if (c) {
return &c->value;
}
@@ -514,7 +514,7 @@ ZEND_API zval *zend_get_constant_ex(zend_string *cname, zend_class_entry *scope,
}
} else {
if (cname) {
- c = zend_get_constant_impl(cname);
+ c = zend_get_constant_ptr(cname);
} else {
c = zend_get_constant_str_impl(name, name_len);
}
diff --git a/Zend/zend_constants.h b/Zend/zend_constants.h
index 33e51858430c1..6f0710c0ce63e 100644
--- a/Zend/zend_constants.h
+++ b/Zend/zend_constants.h
@@ -74,6 +74,7 @@ void zend_startup_constants(void);
void zend_register_standard_constants(void);
ZEND_API bool zend_verify_const_access(zend_class_constant *c, zend_class_entry *ce);
ZEND_API zval *zend_get_constant(zend_string *name);
+ZEND_API zend_constant *zend_get_constant_ptr(zend_string *name);
ZEND_API zval *zend_get_constant_str(const char *name, size_t name_len);
ZEND_API zval *zend_get_constant_ex(zend_string *name, zend_class_entry *scope, uint32_t flags);
ZEND_API zval *zend_get_class_constant_ex(zend_string *class_name, zend_string *constant_name,
zend_class_entry *scope, uint32_t flags);
diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c
index 6f201d85ed233..314a0359d3fc7 100644
--- a/ext/reflection/php_reflection.c
+++ b/ext/reflection/php_reflection.c
@@ -97,6 +97,7 @@ PHPAPI zend_class_entry *reflection_enum_ptr;
PHPAPI zend_class_entry *reflection_enum_unit_case_ptr;
PHPAPI zend_class_entry *reflection_enum_backed_case_ptr;
PHPAPI zend_class_entry *reflection_fiber_ptr;
+PHPAPI zend_class_entry *reflection_constant_ptr;
/* Exception throwing macro */
#define _DO_THROW(msg) \
@@ -538,20 +539,48 @@ static void _class_string(smart_str *str, zend_class_entry *ce, zval *obj,
char
static void _const_string(smart_str *str, char *name, zval *value, char *indent)
{
const char *type = zend_zval_type_name(value);
+ uint32_t flags = Z_CONSTANT_FLAGS_P(value);
+
+ smart_str_appends(str, indent);
+ smart_str_appends(str, "Constant [ ");
+
+ if (flags & (CONST_PERSISTENT|CONST_NO_FILE_CACHE|CONST_DEPRECATED)) {
+ bool first = true;
+ smart_str_appends(str, "<");
+
+#define DUMP_CONST_FLAG(flag, output) \
+ do { \
+ if (flags & flag) { \
+ if (!first) smart_str_appends(str, ", "); \
+ smart_str_appends(str, output); \
+ first = false; \
+ } \
+ } while (0)
+ DUMP_CONST_FLAG(CONST_PERSISTENT, "persistent");
+ DUMP_CONST_FLAG(CONST_NO_FILE_CACHE, "no_file_cache");
+ DUMP_CONST_FLAG(CONST_DEPRECATED, "deprecated");
+#undef DUMP_CONST_FLAG
+
+ smart_str_appends(str, "> ");
+ }
+
+ smart_str_appends(str, type);
+ smart_str_appendc(str, ' ');
+ smart_str_appends(str, name);
+ smart_str_appends(str, " ] { ");
if (Z_TYPE_P(value) == IS_ARRAY) {
- smart_str_append_printf(str, "%s Constant [ %s %s ] { Array }\n",
- indent, type, name);
+ smart_str_append(str, ZSTR_KNOWN(ZEND_STR_ARRAY_CAPITALIZED));
} else if (Z_TYPE_P(value) == IS_STRING) {
- smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
- indent, type, name, Z_STRVAL_P(value));
+ smart_str_appends(str, Z_STRVAL_P(value));
} else {
zend_string *tmp_value_str;
zend_string *value_str = zval_get_tmp_string(value, &tmp_value_str);
- smart_str_append_printf(str, "%s Constant [ %s %s ] { %s }\n",
- indent, type, name, ZSTR_VAL(value_str));
+ smart_str_append(str, value_str);
zend_tmp_string_release(tmp_value_str);
}
+
+ smart_str_appends(str, " }\n");
}
/* }}} */
@@ -1059,7 +1088,7 @@ static void _extension_string(smart_str *str, zend_module_entry *module, char
*i
ZEND_HASH_MAP_FOREACH_PTR(EG(zend_constants), constant) {
if (ZEND_CONSTANT_MODULE_NUMBER(constant) == module->module_number) {
- _const_string(&str_constants, ZSTR_VAL(constant->name), &constant->value,
indent);
+ _const_string(&str_constants, ZSTR_VAL(constant->name), &constant->value, "
");
num_constants++;
}
} ZEND_HASH_FOREACH_END();
@@ -7207,6 +7236,140 @@ static zval *_reflection_write_property(zend_object *object, zend_string
*name,
}
/* }}} */
+ZEND_METHOD(ReflectionConstant, __construct)
+{
+ zend_string *name;
+
+ zval *object = ZEND_THIS;
+ reflection_object *intern = Z_REFLECTION_P(object);
+
+ ZEND_PARSE_PARAMETERS_START(1, 1)
+ Z_PARAM_STR(name)
+ ZEND_PARSE_PARAMETERS_END();
+
+ /* Build name with lowercased ns. */
+ bool backslash_prefixed = ZSTR_VAL(name)[0] == '\\';
+ char *source = ZSTR_VAL(name) + backslash_prefixed;
+ size_t source_len = ZSTR_LEN(name) - backslash_prefixed;
+ zend_string *lc_name = zend_string_alloc(source_len, /* persistent */ false);
+ const char *ns_end = zend_memrchr(source, '\\', source_len);
+ size_t ns_len = 0;
+ if (ns_end) {
+ ns_len = ns_end - ZSTR_VAL(name);
+ zend_str_tolower_copy(ZSTR_VAL(lc_name), source, ns_len);
+ }
+ memcpy(ZSTR_VAL(lc_name) + ns_len, source + ns_len, source_len - ns_len);
+
+ zend_constant *const_ = zend_get_constant_ptr(lc_name);
+ zend_string_release_ex(lc_name, /* persistent */ false);
+ if (!const_) {
+ zend_throw_exception_ex(reflection_exception_ptr, 0, "Constant \"%s\" does not
exist", ZSTR_VAL(name));
+ RETURN_THROWS();
+ }
+
+ intern->ptr = const_;
+ intern->ref_type = REF_TYPE_OTHER;
+
+ zval *name_zv = reflection_prop_name(object);
+ zval_ptr_dtor(name_zv);
+ ZVAL_STR_COPY(name_zv, name);
+}
+
+ZEND_METHOD(ReflectionConstant, getName)
+{
+ reflection_object *intern;
+ zend_constant *const_;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ RETURN_THROWS();
+ }
+
+ GET_REFLECTION_OBJECT_PTR(const_);
+ RETURN_STR_COPY(const_->name);
+}
+
+ZEND_METHOD(ReflectionConstant, getNamespaceName)
+{
+ reflection_object *intern;
+ zend_constant *const_;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ RETURN_THROWS();
+ }
+
+ GET_REFLECTION_OBJECT_PTR(const_);
+
+ const char *backslash = zend_memrchr(ZSTR_VAL(const_->name), '\\',
ZSTR_LEN(const_->name));
+ if (backslash) {
+ size_t length = backslash - ZSTR_VAL(const_->name);
+ RETURN_STRINGL(ZSTR_VAL(const_->name), length);
+ } else {
+ RETURN_EMPTY_STRING();
+ }
+}
+
+ZEND_METHOD(ReflectionConstant, getShortName)
+{
+ reflection_object *intern;
+ zend_constant *const_;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ RETURN_THROWS();
+ }
+
+ GET_REFLECTION_OBJECT_PTR(const_);
+
+ const char *backslash = zend_memrchr(ZSTR_VAL(const_->name), '\\',
ZSTR_LEN(const_->name));
+ if (backslash) {
+ size_t prefix = backslash - ZSTR_VAL(const_->name) + 1;
+ size_t length = ZSTR_LEN(const_->name) - prefix;
+ RETURN_STRINGL(ZSTR_VAL(const_->name) + prefix, length);
+ } else {
+ RETURN_STR_COPY(const_->name);
+ }
+}
+
+ZEND_METHOD(ReflectionConstant, getValue)
+{
+ reflection_object *intern;
+ zend_constant *const_;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ RETURN_THROWS();
+ }
+
+ GET_REFLECTION_OBJECT_PTR(const_);
+ RETURN_COPY(&const_->value);
+}
+
+ZEND_METHOD(ReflectionConstant, isDeprecated)
+{
+ reflection_object *intern;
+ zend_constant *const_;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ RETURN_THROWS();
+ }
+
+ GET_REFLECTION_OBJECT_PTR(const_);
+ RETURN_BOOL(ZEND_CONSTANT_FLAGS(const_) & CONST_DEPRECATED);
+}
+
+ZEND_METHOD(ReflectionConstant, __toString)
+{
+ reflection_object *intern;
+ zend_constant *const_;
+ smart_str str = {0};
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ RETURN_THROWS();
+ }
+
+ GET_REFLECTION_OBJECT_PTR(const_);
+ _const_string(&str, ZSTR_VAL(const_->name), &const_->value, "");
+ RETURN_STR(smart_str_extract(&str));
+}
+
PHP_MINIT_FUNCTION(reflection) /* {{{ */
{
memcpy(&reflection_object_handlers, &std_object_handlers, sizeof(zend_object_handlers));
@@ -7306,6 +7469,10 @@ PHP_MINIT_FUNCTION(reflection) /* {{{ */
reflection_fiber_ptr->create_object = reflection_objects_new;
reflection_fiber_ptr->default_object_handlers = &reflection_object_handlers;
+ reflection_constant_ptr = register_class_ReflectionConstant(reflector_ptr);
+ reflection_constant_ptr->create_object = reflection_objects_new;
+ reflection_constant_ptr->default_object_handlers = &reflection_object_handlers;
+
REFLECTION_G(key_initialized) = 0;
return SUCCESS;
diff --git a/ext/reflection/php_reflection.stub.php b/ext/reflection/php_reflection.stub.php
index 944ae0ad9aeda..c9ce811bfeeb3 100644
--- a/ext/reflection/php_reflection.stub.php
+++ b/ext/reflection/php_reflection.stub.php
@@ -826,3 +826,26 @@ public function getCallable(): callable {}
public function getTrace(int $options = DEBUG_BACKTRACE_PROVIDE_OBJECT): array {}
}
+
+/**
+ * @strict-properties
+ * @not-serializable
+ */
+final class ReflectionConstant implements Reflector
+{
+ public string $name;
+
+ public function __construct(string $name) {}
+
+ public function getName(): string {}
+
+ public function getNamespaceName(): string {}
+
+ public function getShortName(): string {}
+
+ public function getValue(): mixed {}
+
+ public function isDeprecated(): bool {}
+
+ public function __toString(): string {}
+}
diff --git a/ext/reflection/php_reflection_arginfo.h b/ext/reflection/php_reflection_arginfo.h
index 05858203dfc04..e06469db82b3a 100644
--- a/ext/reflection/php_reflection_arginfo.h
+++ b/ext/reflection/php_reflection_arginfo.h
@@ -1,5 +1,5 @@
/* This is a generated file, edit the .stub.php file instead.
- * Stub hash: f787e43effbd114daddad5d3ffcaf9faf7821d64 */
+ * Stub hash: c81572d388f2539d861df717b3cc8c0491fe72a0 */
ZEND_BEGIN_ARG_WITH_TENTATIVE_RETURN_TYPE_INFO_EX(arginfo_class_Reflection_getModifierNames, 0, 1,
IS_ARRAY, 0)
ZEND_ARG_TYPE_INFO(0, modifiers, IS_LONG, 0)
@@ -613,6 +613,21 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionFiber_getTrace,
ZEND_ARG_TYPE_INFO_WITH_DEFAULT_VALUE(0, options, IS_LONG, 0,
"DEBUG_BACKTRACE_PROVIDE_OBJECT")
ZEND_END_ARG_INFO()
+#define arginfo_class_ReflectionConstant___construct arginfo_class_ReflectionExtension___construct
+
+#define arginfo_class_ReflectionConstant_getName arginfo_class_ReflectionFunction___toString
+
+#define arginfo_class_ReflectionConstant_getNamespaceName
arginfo_class_ReflectionFunction___toString
+
+#define arginfo_class_ReflectionConstant_getShortName arginfo_class_ReflectionFunction___toString
+
+ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_class_ReflectionConstant_getValue, 0, 0, IS_MIXED,
0)
+ZEND_END_ARG_INFO()
+
+#define arginfo_class_ReflectionConstant_isDeprecated
arginfo_class_ReflectionFunctionAbstract_hasTentativeReturnType
+
+#define arginfo_class_ReflectionConstant___toString arginfo_class_ReflectionFunction___toString
+
ZEND_METHOD(Reflection, getModifierNames);
ZEND_METHOD(ReflectionClass, __clone);
ZEND_METHOD(ReflectionFunctionAbstract, inNamespace);
@@ -845,6 +860,13 @@ ZEND_METHOD(ReflectionFiber, getExecutingFile);
ZEND_METHOD(ReflectionFiber, getExecutingLine);
ZEND_METHOD(ReflectionFiber, getCallable);
ZEND_METHOD(ReflectionFiber, getTrace);
+ZEND_METHOD(ReflectionConstant, __construct);
+ZEND_METHOD(ReflectionConstant, getName);
+ZEND_METHOD(ReflectionConstant, getNamespaceName);
+ZEND_METHOD(ReflectionConstant, getShortName);
+ZEND_METHOD(ReflectionConstant, getValue);
+ZEND_METHOD(ReflectionConstant, isDeprecated);
+ZEND_METHOD(ReflectionConstant, __toString);
static const zend_function_entry class_ReflectionException_methods[] = {
ZEND_FE_END
@@ -1184,6 +1206,17 @@ static const zend_function_entry class_ReflectionFiber_methods[] = {
ZEND_FE_END
};
+static const zend_function_entry class_ReflectionConstant_methods[] = {
+ ZEND_ME(ReflectionConstant, __construct, arginfo_class_ReflectionConstant___construct,
ZEND_ACC_PUBLIC)
+ ZEND_ME(ReflectionConstant, getName, arginfo_class_ReflectionConstant_getName, ZEND_ACC_PUBLIC)
+ ZEND_ME(ReflectionConstant, getNamespaceName, arginfo_class_ReflectionConstant_getNamespaceName,
ZEND_ACC_PUBLIC)
+ ZEND_ME(ReflectionConstant, getShortName, arginfo_class_ReflectionConstant_getShortName,
ZEND_ACC_PUBLIC)
+ ZEND_ME(ReflectionConstant, getValue, arginfo_class_ReflectionConstant_getValue, ZEND_ACC_PUBLIC)
+ ZEND_ME(ReflectionConstant, isDeprecated, arginfo_class_ReflectionConstant_isDeprecated,
ZEND_ACC_PUBLIC)
+ ZEND_ME(ReflectionConstant, __toString, arginfo_class_ReflectionConstant___toString,
ZEND_ACC_PUBLIC)
+ ZEND_FE_END
+};
+
static zend_class_entry *register_class_ReflectionException(zend_class_entry
*class_entry_Exception)
{
zend_class_entry ce, *class_entry;
@@ -1637,3 +1670,21 @@ static zend_class_entry *register_class_ReflectionFiber(void)
return class_entry;
}
+
+static zend_class_entry *register_class_ReflectionConstant(zend_class_entry *class_entry_Reflector)
+{
+ zend_class_entry ce, *class_entry;
+
+ INIT_CLASS_ENTRY(ce, "ReflectionConstant", class_ReflectionConstant_methods);
+ class_entry = zend_register_internal_class_ex(&ce, NULL);
+ class_entry->ce_flags |=
ZEND_ACC_FINAL|ZEND_ACC_NO_DYNAMIC_PROPERTIES|ZEND_ACC_NOT_SERIALIZABLE;
+ zend_class_implements(class_entry, 1, class_entry_Reflector);
+
+ zval property_name_default_value;
+ ZVAL_UNDEF(&property_name_default_value);
+ zend_string *property_name_name = zend_string_init("name", sizeof("name") - 1,
1);
+ zend_declare_typed_property(class_entry, property_name_name, &property_name_default_value,
ZEND_ACC_PUBLIC, NULL, (zend_type) ZEND_TYPE_INIT_MASK(MAY_BE_STRING));
+ zend_string_release(property_name_name);
+
+ return class_entry;
+}
diff --git a/ext/reflection/tests/ReflectionConstant_double_construct.phpt
b/ext/reflection/tests/ReflectionConstant_double_construct.phpt
new file mode 100644
index 0000000000000..987309d292389
--- /dev/null
+++ b/ext/reflection/tests/ReflectionConstant_double_construct.phpt
@@ -0,0 +1,24 @@
+--TEST--
+ReflectionConstant double construct call
+--FILE--
+<?php
+
+const C1 = 42;
+const C2 = 43;
+
+$r = new ReflectionConstant('C' . mt_rand(1, 1));
+var_dump($r);
+
+$r->__construct('C' . mt_rand(2, 2));
+var_dump($r);
+
+?>
+--EXPECT--
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(2) "C1"
+}
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(2) "C2"
+}
diff --git a/ext/reflection/tests/ReflectionConstant_dump.phpt
b/ext/reflection/tests/ReflectionConstant_dump.phpt
new file mode 100644
index 0000000000000..75f5c9e5dc52a
--- /dev/null
+++ b/ext/reflection/tests/ReflectionConstant_dump.phpt
@@ -0,0 +1,45 @@
+--TEST--
+var_dump(ReflectionConstant)
+--EXTENSIONS--
+zend_test
+--FILE--
+<?php
+
+define('RT_CONST', 42);
+const CT_CONST = [
+ 'foo' => 'foo',
+ 'bar' => ['bar'],
+];
+
+function dump($r) {
+ var_dump($r);
+ echo $r;
+}
+
+dump(new ReflectionConstant('ZEND_CONSTANT_A'));
+dump(new ReflectionConstant('ZEND_TEST_DEPRECATED'));
+dump(new ReflectionConstant('RT_CONST'));
+dump(new ReflectionConstant('CT_CONST'));
+
+?>
+--EXPECT--
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(15) "ZEND_CONSTANT_A"
+}
+Constant [ <persistent> string ZEND_CONSTANT_A ] { global }
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(20) "ZEND_TEST_DEPRECATED"
+}
+Constant [ <persistent, deprecated> int ZEND_TEST_DEPRECATED ] { 42 }
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(8) "RT_CONST"
+}
+Constant [ int RT_CONST ] { 42 }
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(8) "CT_CONST"
+}
+Constant [ array CT_CONST ] { Array }
diff --git a/ext/reflection/tests/ReflectionConstant_getName.phpt
b/ext/reflection/tests/ReflectionConstant_getName.phpt
new file mode 100644
index 0000000000000..24e22398d6582
--- /dev/null
+++ b/ext/reflection/tests/ReflectionConstant_getName.phpt
@@ -0,0 +1,27 @@
+--TEST--
+ReflectionConstant::getName()
+--EXTENSIONS--
+zend_test
+--FILE--
+<?php
+
+$reflectionConstant = new ReflectionConstant('ZEND_CONSTANT_A');
+var_dump($reflectionConstant->getName());
+
+$reflectionConstant = new ReflectionConstant('ZEND_TEST_DEPRECATED');
+var_dump($reflectionConstant->getName());
+
+define('RT_CONST', 42);
+$reflectionConstant = new ReflectionConstant('RT_CONST');
+var_dump($reflectionConstant->getName());
+
+define('CT_CONST', 43);
+$reflectionConstant = new ReflectionConstant('CT_CONST');
+var_dump($reflectionConstant->getName());
+
+?>
+--EXPECT--
+string(15) "ZEND_CONSTANT_A"
+string(20) "ZEND_TEST_DEPRECATED"
+string(8) "RT_CONST"
+string(8) "CT_CONST"
diff --git a/ext/reflection/tests/ReflectionConstant_getValue.phpt
b/ext/reflection/tests/ReflectionConstant_getValue.phpt
new file mode 100644
index 0000000000000..54d53a0304ffa
--- /dev/null
+++ b/ext/reflection/tests/ReflectionConstant_getValue.phpt
@@ -0,0 +1,27 @@
+--TEST--
+ReflectionConstant::getValue()
+--EXTENSIONS--
+zend_test
+--FILE--
+<?php
+
+$reflectionConstant = new ReflectionConstant('ZEND_CONSTANT_A');
+var_dump($reflectionConstant->getValue());
+
+$reflectionConstant = new ReflectionConstant('ZEND_TEST_DEPRECATED');
+var_dump($reflectionConstant->getValue());
+
+define('RT_CONST', 42);
+$reflectionConstant = new ReflectionConstant('RT_CONST');
+var_dump($reflectionConstant->getValue());
+
+define('CT_CONST', 43);
+$reflectionConstant = new ReflectionConstant('CT_CONST');
+var_dump($reflectionConstant->getValue());
+
+?>
+--EXPECT--
+string(6) "global"
+int(42)
+int(42)
+int(43)
diff --git a/ext/reflection/tests/ReflectionConstant_inexistent.phpt
b/ext/reflection/tests/ReflectionConstant_inexistent.phpt
new file mode 100644
index 0000000000000..19d038ae12d07
--- /dev/null
+++ b/ext/reflection/tests/ReflectionConstant_inexistent.phpt
@@ -0,0 +1,14 @@
+--TEST--
+ReflectionConstant on inexistent constant
+--FILE--
+<?php
+
+try {
+ new \ReflectionConstant('C');
+} catch (Exception $e) {
+ echo get_class($e) . ': ' . $e->getMessage() . "\n";
+}
+
+?>
+--EXPECT--
+ReflectionException: Constant "C" does not exist
diff --git a/ext/reflection/tests/ReflectionConstant_isDeprecated.phpt
b/ext/reflection/tests/ReflectionConstant_isDeprecated.phpt
new file mode 100644
index 0000000000000..9a88dbe6528fb
--- /dev/null
+++ b/ext/reflection/tests/ReflectionConstant_isDeprecated.phpt
@@ -0,0 +1,27 @@
+--TEST--
+ReflectionConstant::isDeprecated()
+--EXTENSIONS--
+zend_test
+--FILE--
+<?php
+
+$reflectionConstant = new ReflectionConstant('ZEND_CONSTANT_A');
+var_dump($reflectionConstant->isDeprecated());
+
+$reflectionConstant = new ReflectionConstant('ZEND_TEST_DEPRECATED');
+var_dump($reflectionConstant->isDeprecated());
+
+define('RT_CONST', 42);
+$reflectionConstant = new ReflectionConstant('RT_CONST');
+var_dump($reflectionConstant->isDeprecated());
+
+const CT_CONST = 43;
+$reflectionConstant = new ReflectionConstant('CT_CONST');
+var_dump($reflectionConstant->isDeprecated());
+
+?>
+--EXPECT--
+bool(false)
+bool(true)
+bool(false)
+bool(false)
diff --git a/ext/reflection/tests/ReflectionConstant_ns.phpt
b/ext/reflection/tests/ReflectionConstant_ns.phpt
new file mode 100644
index 0000000000000..b047fc25dcf29
--- /dev/null
+++ b/ext/reflection/tests/ReflectionConstant_ns.phpt
@@ -0,0 +1,52 @@
+--TEST--
+ReflectionConstant with namespace
+--FILE--
+<?php
+
+namespace Foo {
+ const C = 42;
+}
+
+namespace {
+ const C = 43;
+
+ var_dump(new \ReflectionConstant('C'));
+ var_dump(new \ReflectionConstant('\\C'));
+ var_dump(new \ReflectionConstant('Foo\\C'));
+ var_dump(new \ReflectionConstant('\\Foo\\C'));
+ var_dump((new \ReflectionConstant('C'))->getNamespaceName());
+ var_dump((new \ReflectionConstant('\\C'))->getNamespaceName());
+ var_dump((new \ReflectionConstant('Foo\\C'))->getNamespaceName());
+ var_dump((new \ReflectionConstant('\\Foo\\C'))->getNamespaceName());
+ var_dump((new \ReflectionConstant('C'))->getShortName());
+ var_dump((new \ReflectionConstant('\\C'))->getShortName());
+ var_dump((new \ReflectionConstant('Foo\\C'))->getShortName());
+ var_dump((new \ReflectionConstant('\\Foo\\C'))->getShortName());
+}
+
+?>
+--EXPECT--
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(1) "C"
+}
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(2) "\C"
+}
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(5) "Foo\C"
+}
+object(ReflectionConstant)#1 (1) {
+ ["name"]=>
+ string(6) "\Foo\C"
+}
+string(0) ""
+string(0) ""
+string(3) "Foo"
+string(3) "Foo"
+string(1) "C"
+string(1) "C"
+string(1) "C"
+string(1) "C"
diff --git a/ext/reflection/tests/ReflectionExtension_getClasses_basic.phpt
b/ext/reflection/tests/ReflectionExtension_getClasses_basic.phpt
index b7c10079d4dd3..b7537f170a502 100644
--- a/ext/reflection/tests/ReflectionExtension_getClasses_basic.phpt
+++ b/ext/reflection/tests/ReflectionExtension_getClasses_basic.phpt
@@ -8,7 +8,7 @@ $ext = new ReflectionExtension('reflection');
var_dump($ext->getClasses());
?>
--EXPECT--
-array(24) {
+array(25) {
["ReflectionException"]=>
object(ReflectionClass)#2 (1) {
["name"]=>
@@ -129,4 +129,9 @@ array(24) {
["name"]=>
string(15) "ReflectionFiber"
}
+ ["ReflectionConstant"]=>
+ object(ReflectionClass)#26 (1) {
+ ["name"]=>
+ string(18) "ReflectionConstant"
+ }
}
diff --git a/sapi/cli/tests/006.phpt b/sapi/cli/tests/006.phpt
index 9d693f65969b4..38c117ec70f2f 100644
--- a/sapi/cli/tests/006.phpt
+++ b/sapi/cli/tests/006.phpt
@@ -43,25 +43,25 @@ string(%d) "Extension [ <persistent> extension #%d pcre version %s ]
{
}
- Constants [19] {
- Constant [ int PREG_PATTERN_ORDER ] { 1 }
- Constant [ int PREG_SET_ORDER ] { 2 }
- Constant [ int PREG_OFFSET_CAPTURE ] { 256 }
- Constant [ int PREG_UNMATCHED_AS_NULL ] { 512 }
- Constant [ int PREG_SPLIT_NO_EMPTY ] { 1 }
- Constant [ int PREG_SPLIT_DELIM_CAPTURE ] { 2 }
- Constant [ int PREG_SPLIT_OFFSET_CAPTURE ] { 4 }
- Constant [ int PREG_GREP_INVERT ] { 1 }
- Constant [ int PREG_NO_ERROR ] { 0 }
- Constant [ int PREG_INTERNAL_ERROR ] { 1 }
- Constant [ int PREG_BACKTRACK_LIMIT_ERROR ] { 2 }
- Constant [ int PREG_RECURSION_LIMIT_ERROR ] { 3 }
- Constant [ int PREG_BAD_UTF8_ERROR ] { 4 }
- Constant [ int PREG_BAD_UTF8_OFFSET_ERROR ] { 5 }
- Constant [ int PREG_JIT_STACKLIMIT_ERROR ] { 6 }
- Constant [ string PCRE_VERSION ] { %s }
- Constant [ int PCRE_VERSION_MAJOR ] { %d }
- Constant [ int PCRE_VERSION_MINOR ] { %d }
- Constant [ bool PCRE_JIT_SUPPORT ] { %d }
+ Constant [ <persistent> int PREG_PATTERN_ORDER ] { 1 }
+ Constant [ <persistent> int PREG_SET_ORDER ] { 2 }
+ Constant [ <persistent> int PREG_OFFSET_CAPTURE ] { 256 }
+ Constant [ <persistent> int PREG_UNMATCHED_AS_NULL ] { 512 }
+ Constant [ <persistent> int PREG_SPLIT_NO_EMPTY ] { 1 }
+ Constant [ <persistent> int PREG_SPLIT_DELIM_CAPTURE ] { 2 }
+ Constant [ <persistent> int PREG_SPLIT_OFFSET_CAPTURE ] { 4 }
+ Constant [ <persistent> int PREG_GREP_INVERT ] { 1 }
+ Constant [ <persistent> int PREG_NO_ERROR ] { 0 }
+ Constant [ <persistent> int PREG_INTERNAL_ERROR ] { 1 }
+ Constant [ <persistent> int PREG_BACKTRACK_LIMIT_ERROR ] { 2 }
+ Constant [ <persistent> int PREG_RECURSION_LIMIT_ERROR ] { 3 }
+ Constant [ <persistent> int PREG_BAD_UTF8_ERROR ] { 4 }
+ Constant [ <persistent> int PREG_BAD_UTF8_OFFSET_ERROR ] { 5 }
+ Constant [ <persistent> int PREG_JIT_STACKLIMIT_ERROR ] { 6 }
+ Constant [ <persistent> string PCRE_VERSION ] { %s }
+ Constant [ <persistent> int PCRE_VERSION_MAJOR ] { %d }
+ Constant [ <persistent> int PCRE_VERSION_MINOR ] { %d }
+ Constant [ <persistent> bool PCRE_JIT_SUPPORT ] { %d }
}
- Functions {