summaryrefslogtreecommitdiff
path: root/ext/reflection/php_reflection.c
diff options
context:
space:
mode:
authorBenjamin Eberlei <kontakt@beberlei.de>2020-01-09 22:51:15 +0100
committerBenjamin Eberlei <kontakt@beberlei.de>2020-01-22 21:24:36 +0100
commit7a52bf21d358567980bc20a95cef730bc9e9f00d (patch)
tree55aca354d3c7dc301294fedc4be4dfd78d5269e0 /ext/reflection/php_reflection.c
parent476636841f450cf90c4a2a9516160303f0df3cb7 (diff)
downloadphp-git-7a52bf21d358567980bc20a95cef730bc9e9f00d.tar.gz
Add ReflectionProperty::getDefaultValue and ReflectionProperty::hasDefaultValue
Diffstat (limited to 'ext/reflection/php_reflection.c')
-rw-r--r--ext/reflection/php_reflection.c86
1 files changed, 86 insertions, 0 deletions
diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c
index d53862fba7..e1c41e5ec5 100644
--- a/ext/reflection/php_reflection.c
+++ b/ext/reflection/php_reflection.c
@@ -5686,6 +5686,90 @@ ZEND_METHOD(reflection_property, hasType)
}
/* }}} */
+/* {{{ proto public bool ReflectionProperty::hasDefaultValue()
+ Returns whether property has a default value */
+ZEND_METHOD(reflection_property, hasDefaultValue)
+{
+ reflection_object *intern;
+ property_reference *ref;
+ zend_property_info *prop_info;
+ zval *prop;
+ zend_class_entry *ce;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ RETURN_THROWS();
+ }
+
+ GET_REFLECTION_OBJECT_PTR(ref);
+
+ prop_info = ref->prop;
+
+ if (prop_info == NULL) {
+ RETURN_FALSE;
+ }
+
+ ce = prop_info->ce;
+
+ if ((prop_info->flags & ZEND_ACC_STATIC) != 0) {
+ prop = &ce->default_static_members_table[prop_info->offset];
+ ZVAL_DEINDIRECT(prop);
+ } else {
+ prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
+ }
+
+ RETURN_BOOL(!Z_ISUNDEF_P(prop));
+}
+/* }}} */
+
+/* {{{ proto public mixed ReflectionProperty::getDefaultValue()
+ Returns the default value of a property */
+ZEND_METHOD(reflection_property, getDefaultValue)
+{
+ reflection_object *intern;
+ property_reference *ref;
+ zend_property_info *prop_info;
+ zval *prop;
+ zend_class_entry *ce;
+
+ if (zend_parse_parameters_none() == FAILURE) {
+ RETURN_THROWS();
+ }
+
+ GET_REFLECTION_OBJECT_PTR(ref);
+
+ prop_info = ref->prop;
+
+ if (prop_info == NULL) {
+ return; // throw exception?
+ }
+
+ ce = prop_info->ce;
+
+ if ((prop_info->flags & ZEND_ACC_STATIC) != 0) {
+ prop = &ce->default_static_members_table[prop_info->offset];
+ ZVAL_DEINDIRECT(prop);
+ } else {
+ prop = &ce->default_properties_table[OBJ_PROP_TO_NUM(prop_info->offset)];
+ }
+
+ if (Z_ISUNDEF_P(prop)) {
+ return;
+ }
+
+ /* copy: enforce read only access */
+ ZVAL_DEREF(prop);
+ ZVAL_COPY_OR_DUP(return_value, prop);
+
+ /* this is necessary to make it able to work with default array
+ * properties, returned to user */
+ if (Z_TYPE_P(return_value) == IS_CONSTANT_AST) {
+ if (UNEXPECTED(zval_update_constant_ex(return_value, ce) != SUCCESS)) {
+ RETURN_THROWS();
+ }
+ }
+}
+/* }}} */
+
/* {{{ proto public static mixed ReflectionExtension::export(string name [, bool return]) throws ReflectionException
Exports a reflection object. Returns the output if TRUE is specified for return, printing it otherwise. */
ZEND_METHOD(reflection_extension, export)
@@ -6461,6 +6545,8 @@ static const zend_function_entry reflection_property_functions[] = {
ZEND_ME(reflection_property, setAccessible, arginfo_class_ReflectionProperty_setAccessible, 0)
ZEND_ME(reflection_property, getType, arginfo_class_ReflectionProperty_getType, 0)
ZEND_ME(reflection_property, hasType, arginfo_class_ReflectionProperty_hasType, 0)
+ ZEND_ME(reflection_property, hasDefaultValue, arginfo_class_ReflectionProperty_hasDefaultValue, 0)
+ ZEND_ME(reflection_property, getDefaultValue, arginfo_class_ReflectionProperty_getDefaultValue, 0)
PHP_FE_END
};