From: Johannes Schlüter Date: Sat, 29 May 2010 20:40:58 +0000 (+0000) Subject: - Make reflection aware of traits X-Git-Tag: php-5.4.0alpha1~191^2~1398 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=19afc82e283ca0114117621904bf38982cccfed2;p=php - Make reflection aware of traits --- diff --git a/ext/reflection/php_reflection.c b/ext/reflection/php_reflection.c index dd33452def..87e8e52eba 100644 --- a/ext/reflection/php_reflection.c +++ b/ext/reflection/php_reflection.c @@ -355,7 +355,13 @@ static void _class_string(string *str, zend_class_entry *ce, zval *obj, char *in if (obj) { string_printf(str, "%sObject of class [ ", indent); } else { - string_printf(str, "%s%s [ ", indent, (ce->ce_flags & ZEND_ACC_INTERFACE) ? "Interface" : "Class"); + char *kind = "Class"; + if (ce->ce_flags & ZEND_ACC_INTERFACE) { + kind = "Interface"; + } else if (ce->ce_flags & ZEND_ACC_TRAIT) { + kind = "Trait"; + } + string_printf(str, "%s%s [ ", indent, kind); } string_printf(str, (ce->type == ZEND_USER_CLASS) ? "module) { @@ -367,6 +373,8 @@ static void _class_string(string *str, zend_class_entry *ce, zval *obj, char *in } if (ce->ce_flags & ZEND_ACC_INTERFACE) { string_printf(str, "interface "); + } else if (ce->ce_flags & ZEND_ACC_TRAIT) { + string_printf(str, "trait "); } else { if (ce->ce_flags & (ZEND_ACC_IMPLICIT_ABSTRACT_CLASS|ZEND_ACC_EXPLICIT_ABSTRACT_CLASS)) { string_printf(str, "abstract "); @@ -3974,6 +3982,14 @@ ZEND_METHOD(reflection_class, isInterface) } /* }}} */ +/* {{{ proto public bool ReflectionClass::isTrait() + Returns whether this is a trait */ +ZEND_METHOD(reflection_class, isTrait) +{ + _class_check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_TRAIT); +} +/* }}} */ + /* {{{ proto public bool ReflectionClass::isFinal() Returns whether this class is final */ ZEND_METHOD(reflection_class, isFinal) @@ -5605,6 +5621,7 @@ static const zend_function_entry reflection_class_functions[] = { ZEND_ME(reflection_class, getInterfaces, arginfo_reflection__void, 0) ZEND_ME(reflection_class, getInterfaceNames, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isInterface, arginfo_reflection__void, 0) + ZEND_ME(reflection_class, isTrait, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isAbstract, arginfo_reflection__void, 0) ZEND_ME(reflection_class, isFinal, arginfo_reflection__void, 0) ZEND_ME(reflection_class, getModifiers, arginfo_reflection__void, 0) diff --git a/ext/reflection/tests/ReflectionClass_toString_001.phpt b/ext/reflection/tests/ReflectionClass_toString_001.phpt index 6173cf0c0a..790ee4e5e3 100644 --- a/ext/reflection/tests/ReflectionClass_toString_001.phpt +++ b/ext/reflection/tests/ReflectionClass_toString_001.phpt @@ -34,7 +34,7 @@ Class [ class ReflectionClass implements Reflector ] { Property [ public $name ] } - - Methods [43] { + - Methods [44] { Method [ final private method __clone ] { - Parameters [0] { @@ -188,6 +188,12 @@ Class [ class ReflectionClass implements Reflector ] { } } + Method [ public method isTrait ] { + + - Parameters [0] { + } + } + Method [ public method isAbstract ] { - Parameters [0] { diff --git a/ext/reflection/tests/traits001.phpt b/ext/reflection/tests/traits001.phpt new file mode 100644 index 0000000000..511ade56fa --- /dev/null +++ b/ext/reflection/tests/traits001.phpt @@ -0,0 +1,71 @@ +--TEST-- +ReflectionClass and Traits +--FILE-- +isTrait()); +var_dump($rBar->isTrait()); +echo $rFoo; +echo $rBar; +--EXPECTF-- +bool(true) +bool(false) +Trait [ trait Foo ] { + @@ %straits001.php 2-4 + + - Constants [0] { + } + + - Static properties [0] { + } + + - Static methods [0] { + } + + - Properties [0] { + } + + - Methods [1] { + Method [ public method someMethod ] { + @@ %straits001.php 3 - 3 + } + } +} +Class [ class Bar ] { + @@ %straits001.php 6-10 + + - Constants [0] { + } + + - Static properties [0] { + } + + - Static methods [0] { + } + + - Properties [0] { + } + + - Methods [2] { + Method [ public method someOtherMethod ] { + @@ %straits001.php 9 - 9 + } + + + Method [ public method someMethod ] { + @@ %straits001.php 3 - 3 + } + } +}