Newsgroups: php.internals Path: news.php.net Xref: news.php.net php.internals:3093 Return-Path: Mailing-List: contact internals-help@lists.php.net; run by ezmlm Delivered-To: mailing list internals@lists.php.net Received: (qmail 20276 invoked from network); 1 Jul 2003 18:26:22 -0000 Received: from unknown (HELO moutng.kundenserver.de) (212.227.126.177) by pb1.pair.com with SMTP; 1 Jul 2003 18:26:22 -0000 Received: from [212.227.126.162] (helo=mrelayng.kundenserver.de) by moutng.kundenserver.de with esmtp (Exim 3.35 #1) id 19XPpc-0004KZ-00; Tue, 01 Jul 2003 20:26:20 +0200 Received: from [217.80.180.191] (helo=[217.80.180.191]) by mrelayng.kundenserver.de with asmtp (Exim 3.35 #1) id 19XPpa-0007ju-00; Tue, 01 Jul 2003 20:26:18 +0200 To: engine2@lists.zend.com Cc: internals@lists.php.net Content-Type: multipart/mixed; boundary="=-e8syufE3y9w9J5tddI6I" Organization: Message-ID: <1057084048.43882.22.camel@localhost> Mime-Version: 1.0 X-Mailer: Ximian Evolution 1.2.4 Date: 01 Jul 2003 20:27:28 +0200 Subject: Work on reflection API From: thekid@thekid.de (Timm Friebe) --=-e8syufE3y9w9J5tddI6I Content-Type: text/plain Content-Transfer-Encoding: 7bit Hello, I went ahead and spent some time on the new reflection API. A patch (778 lines:)) and a demo script are attached. What has changed: --------------------------------------------------------------------- - Added documentation for each and every function (using the style used in ext/*/*.c), e.g.: /* {{{ proto Reflection_Class[] Reflection_Class::getInterfaces() Returns an array of interfaces this class implements */ - Replaced duplicated code with macros or functions - Added protection against static calls to the Reflection_* classes' functions (resulting in fatal errors if you try to do so). - Made Reflection_Class::getInterfaces return an empty array instead of FALSE in case the class does not implement any interfaces - Made Reflection_Function::getStaticVariables return an empty array instead of FALSE in case the function does not have any static variables. - Made use of the ZEND_DO_THROW macro in the constructors (see previous mail to engine2@ including a patch to zend_API.h) - Added Reflection_Class::isFinal - Added Reflection_Class::isAbstract - Added Reflection_Class::newInstance. This method takes a variable amount of arguments and passes them to the object's constructor if there is one. - Added Reflection_Class::isInstance --------------------------------------------------------------------- I couldn't figure out how to remove the following memleaks which occur when the reflection_class_factory is called: /usr/home/thekid/devel/php/php/Zend/zend_reflection_api.c(296) : Freeing 0x08371D24 (7 bytes), script=reflection.php Last leak repeated 1 time /usr/home/thekid/devel/php/php/Zend/zend_reflection_api.c(295) : Freeing 0x08371CE4 (16 bytes), script=reflection.php Last leak repeated 1 time -- Timm --=-e8syufE3y9w9J5tddI6I Content-Disposition: attachment; filename=zend_reflection_api.c.diff Content-Type: text/x-patch; name=zend_reflection_api.c.diff; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit Index: Zend/zend_reflection_api.c =================================================================== RCS file: /repository/ZendEngine2/zend_reflection_api.c,v retrieving revision 1.6 diff -u -r1.6 zend_reflection_api.c --- Zend/zend_reflection_api.c 1 Jul 2003 04:25:29 -0000 1.6 +++ Zend/zend_reflection_api.c 1 Jul 2003 18:22:57 -0000 @@ -24,7 +24,23 @@ #include "zend_default_classes.h" #include "zend_operators.h" -extern zend_class_entry *default_exception_ptr; +#define METHOD_NOTSTATIC \ + if (!this_ptr) { \ + zend_error(E_ERROR, "%s() cannot be called statically", get_active_function_name(TSRMLS_C)); \ + return; \ + } \ + +#define METHOD_NOTSTATIC_NUMPARAMS(c) METHOD_NOTSTATIC \ + if (ZEND_NUM_ARGS() > c) { \ + ZEND_WRONG_PARAM_COUNT(); \ + } \ + +#define GET_REFLECTION_OBJECT_PTR(target) \ + intern = (reflection_object *) zend_object_store_get_object(getThis() TSRMLS_CC); \ + if (intern == NULL || (target = intern->ptr) == NULL) { \ + zend_error(E_ERROR, "Internal error: Failed to retrieve the reflection object"); \ + } \ + zend_class_entry *reflection_function_ptr; zend_class_entry *reflection_class_ptr; @@ -94,6 +110,8 @@ return object; } +/* {{{ proto Reflection_Function Reflection_Function::__construct(string name) + Constructor. Throws an Exception in case the given function does not exist */ ZEND_FUNCTION(reflection_function) { zval **name; @@ -104,226 +122,163 @@ int argc = ZEND_NUM_ARGS(); if (zend_get_parameters_ex(argc, &name) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); + ZEND_WRONG_PARAM_COUNT(); } object = getThis(); intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); if (intern == NULL) { - return; + return; } convert_to_string_ex(name); zval_add_ref(name); zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) name, sizeof(zval *), NULL); lcname = zend_str_tolower_dup((const char *)Z_STRVAL_PP(name), (int) Z_STRLEN_PP(name)); if (zend_hash_find(EG(function_table), lcname, (int)(Z_STRLEN_PP(name) + 1), (void **)&fptr) == FAILURE) { - zval *ex; - zval *tmp; - MAKE_STD_ZVAL(ex); - object_init_ex(ex, default_exception_ptr); - - MAKE_STD_ZVAL(tmp); - ZVAL_STRING(tmp, "Function does not exist", 1); - zend_hash_update(Z_OBJPROP_P(ex), "message", sizeof("message"), (void **) &tmp, sizeof(zval *), NULL); - tmp = NULL; - - MAKE_STD_ZVAL(tmp); - ZVAL_STRING(tmp, zend_get_executed_filename(TSRMLS_C), 1); - zend_hash_update(Z_OBJPROP_P(ex), "file", sizeof("file"), (void **) &tmp, sizeof(zval *), NULL); - tmp = NULL; - - MAKE_STD_ZVAL(tmp); - ZVAL_LONG(tmp, zend_get_executed_lineno(TSRMLS_C)); - zend_hash_update(Z_OBJPROP_P(ex), "line", sizeof("line"), (void **) &tmp, sizeof(zval *), NULL); - tmp = NULL; - - EG(exception) = ex; + ZEND_DO_THROW("Function does not exist"); } efree(lcname); intern->ptr = fptr; } +/* }}} */ +/* {{{ proto string Reflection_Function::getName() + Returns this function's name */ ZEND_FUNCTION(reflection_function_getname) { - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } + METHOD_NOTSTATIC_NUMPARAMS(0); _default_get_entry(getThis(), "name", sizeof("name"), return_value TSRMLS_CC); } +/* }}} */ +/* {{{ proto bool Reflection_Function::isInternal() + Returns whether this is an internal function */ ZEND_FUNCTION(reflection_function_isinternal) { - zval *object; reflection_object *intern; zend_function *fptr; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL) { - RETURN_FALSE; - } - if ((fptr = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(fptr); RETURN_BOOL(fptr->type == ZEND_INTERNAL_FUNCTION); } +/* }}} */ +/* {{{ proto bool Reflection_Function::isUserDefined() + Returns whether this is an user-defined function */ ZEND_FUNCTION(reflection_function_isuserdefined) { - zval *object; reflection_object *intern; zend_function *fptr; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL) { - return; - } - if ((fptr = intern->ptr) == NULL) { - return; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(fptr); RETURN_BOOL(fptr->type == ZEND_USER_FUNCTION); } +/* }}} */ +/* {{{ proto string Reflection_Function::getFileName() + Returns the filename of the file this function was declared in */ ZEND_FUNCTION(reflection_function_getfilename) { - zval *object; reflection_object *intern; zend_function *fptr; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL) { - return; - } - if ((fptr = intern->ptr) == NULL) { - return; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(fptr); if (fptr->type == ZEND_USER_FUNCTION) { RETURN_STRING(fptr->op_array.filename, 1); } RETURN_FALSE; } +/* }}} */ +/* {{{ proto int Reflection_Function::getStartLine() + Returns the line this function's declaration starts at */ ZEND_FUNCTION(reflection_function_getstartline) { - zval *object; reflection_object *intern; zend_function *fptr; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL) { - return; - } - if ((fptr = intern->ptr) == NULL) { - return; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(fptr); if (fptr->type == ZEND_USER_FUNCTION) { RETURN_LONG(fptr->op_array.line_start); } RETURN_FALSE; } +/* }}} */ +/* {{{ proto int Reflection_Function::getEndLine() + Returns the line this function's declaration ends at */ ZEND_FUNCTION(reflection_function_getendline) { - zval *object; reflection_object *intern; zend_function *fptr; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL) { - RETURN_FALSE; - } - if ((fptr = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(fptr); if (fptr->type == ZEND_USER_FUNCTION) { RETURN_LONG(fptr->op_array.line_end); } RETURN_FALSE; } +/* }}} */ +/* {{{ proto string Reflection_Function::getDocComment() + Returns the doc comment for this function */ ZEND_FUNCTION(reflection_function_getdoccomment) { - zval *object; reflection_object *intern; zend_function *fptr; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL) { - RETURN_FALSE; - } - if ((fptr = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(fptr); if (fptr->type == ZEND_USER_FUNCTION && fptr->op_array.doc_comment) { RETURN_STRINGL(fptr->op_array.doc_comment, fptr->op_array.doc_comment_len, 1); } RETURN_FALSE; } +/* }}} */ +/* {{{ proto array Reflection_Function::getStaticVariables() + Returns an associative array containing this function's static variables and their values */ ZEND_FUNCTION(reflection_function_getstaticvariables) { - zval *object, *tmp_copy; + zval *tmp_copy; reflection_object *intern; zend_function *fptr; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL) { - RETURN_FALSE; - } - if ((fptr = intern->ptr) == NULL || fptr->op_array.static_variables == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(fptr); + + /* Return an empty array in case no static variables exist */ array_init(return_value); - zend_hash_copy(Z_ARRVAL_P(return_value), fptr->op_array.static_variables, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *)); + if (fptr->op_array.static_variables != NULL) { + zend_hash_copy(Z_ARRVAL_P(return_value), fptr->op_array.static_variables, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *)); + } } +/* }}} */ +/* {{{ proto mixed Reflection_Function::invoke(mixed* args) + Invokes the function */ ZEND_FUNCTION(reflection_function_invoke) { - zval *object, *retval_ptr; + zval *retval_ptr; zval ***params; reflection_object *intern; zend_function *fptr; int argc = ZEND_NUM_ARGS(); - object = getThis(); + METHOD_NOTSTATIC; + GET_REFLECTION_OBJECT_PTR(fptr); + params = safe_emalloc(sizeof(zval **), argc, 0); if (zend_get_parameters_array_ex(argc, params) == FAILURE) { efree(params); RETURN_FALSE; } - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (fptr = intern->ptr) == NULL) { - efree(params); - RETURN_FALSE; - } if (fast_call_user_function(EG(function_table), NULL, NULL, &retval_ptr, argc, params, 1, NULL, &fptr TSRMLS_CC) == SUCCESS && retval_ptr) { @@ -331,6 +286,7 @@ } efree(params); } +/* }}} */ void reflection_class_factory(zend_class_entry *ce, zval *object TSRMLS_DC) { @@ -344,6 +300,8 @@ zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) &name, sizeof(zval *), NULL); } +/* {{{ proto Reflection_Class Reflection_Class::__construct(string name) + Constructor. Throws an Exception in case the given class does not exist */ ZEND_FUNCTION(reflection_class) { zval **name; @@ -354,244 +312,270 @@ int argc = ZEND_NUM_ARGS(); if (zend_get_parameters_ex(argc, &name) == FAILURE) { - ZEND_WRONG_PARAM_COUNT(); + ZEND_WRONG_PARAM_COUNT(); } object = getThis(); intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); if (intern == NULL) { - return; + return; } convert_to_string_ex(name); zval_add_ref(name); zend_hash_update(Z_OBJPROP_P(object), "name", sizeof("name"), (void **) name, sizeof(zval *), NULL); lcname = zend_str_tolower_dup((const char *)Z_STRVAL_PP(name), (int) Z_STRLEN_PP(name)); if (zend_hash_find(EG(class_table), lcname, (int)(Z_STRLEN_PP(name) + 1), (void **)&ce) == FAILURE) { - zval *ex; - zval *tmp; - MAKE_STD_ZVAL(ex); - object_init_ex(ex, default_exception_ptr); - - MAKE_STD_ZVAL(tmp); - ZVAL_STRING(tmp, "Function does not exist", 1); - zend_hash_update(Z_OBJPROP_P(ex), "message", sizeof("message"), (void **) &tmp, sizeof(zval *), NULL); - tmp = NULL; - - MAKE_STD_ZVAL(tmp); - ZVAL_STRING(tmp, zend_get_executed_filename(TSRMLS_C), 1); - zend_hash_update(Z_OBJPROP_P(ex), "file", sizeof("file"), (void **) &tmp, sizeof(zval *), NULL); - tmp = NULL; - - MAKE_STD_ZVAL(tmp); - ZVAL_LONG(tmp, zend_get_executed_lineno(TSRMLS_C)); - zend_hash_update(Z_OBJPROP_P(ex), "line", sizeof("line"), (void **) &tmp, sizeof(zval *), NULL); - tmp = NULL; - - EG(exception) = ex; + ZEND_DO_THROW("Class does not exist"); } efree(lcname); intern->ptr = *ce; } +/* }}} */ +/* {{{ proto string Reflection_Class::getName() + Returns the class' name */ ZEND_FUNCTION(reflection_class_getname) { - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } + METHOD_NOTSTATIC_NUMPARAMS(0); _default_get_entry(getThis(), "name", sizeof("name"), return_value TSRMLS_CC); } +/* }}} */ +/* {{{ proto bool Reflection_Class::isInternal() + Returns whether this class is an internal class */ ZEND_FUNCTION(reflection_class_isinternal) { - zval *object; reflection_object *intern; zend_class_entry *ce; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (ce = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); RETURN_BOOL(ce->type == ZEND_INTERNAL_CLASS); } +/* }}} */ +/* {{{ proto bool Reflection_Class::isUserDefined() + Returns whether this class is user-defined */ ZEND_FUNCTION(reflection_class_isuserdefined) { - zval *object; reflection_object *intern; zend_class_entry *ce; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (ce = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); RETURN_BOOL(ce->type == ZEND_USER_CLASS); } +/* }}} */ +/* {{{ proto string Reflection_Class::getFileName() + Returns the filename of the file this class was declared in */ ZEND_FUNCTION(reflection_class_getfilename) { - zval *object; reflection_object *intern; zend_class_entry *ce; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (ce = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); if (ce->type == ZEND_USER_CLASS) { RETURN_STRING(ce->filename, 1); } RETURN_FALSE; } +/* }}} */ +/* {{{ proto int Reflection_Class::getStartLine() + Returns the line this class' declaration starts at */ ZEND_FUNCTION(reflection_class_getstartline) { - zval *object; reflection_object *intern; zend_class_entry *ce; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || ((ce = intern->ptr) == NULL)) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); if (ce->type == ZEND_USER_FUNCTION) { RETURN_LONG(ce->line_start); } RETURN_FALSE; } +/* }}} */ +/* {{{ proto int Reflection_Class::getEndLine() + Returns the line this class' declaration ends at */ ZEND_FUNCTION(reflection_class_getendline) { - zval *object; reflection_object *intern; zend_class_entry *ce; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (ce = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); if (ce->type == ZEND_USER_CLASS) { RETURN_LONG(ce->line_end); } RETURN_FALSE; } +/* }}} */ +/* {{{ proto string Reflection_Class::getDocComment() + Returns the doc comment for this class */ ZEND_FUNCTION(reflection_class_getdoccomment) { - zval *object; reflection_object *intern; zend_class_entry *ce; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL && (ce = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); if (ce->type == ZEND_USER_CLASS && ce->doc_comment) { RETURN_STRINGL(ce->doc_comment, ce->doc_comment_len, 1); } RETURN_FALSE; } +/* }}} */ +/* {{{ proto array Reflection_Class::getConstants() + Returns an associative array containing this class' constants and their values */ ZEND_FUNCTION(reflection_class_getconstants) { - zval *object, *tmp_copy; + zval *tmp_copy; reflection_object *intern; zend_class_entry *ce; - - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (ce = intern->ptr) == NULL) { - RETURN_FALSE; - } + + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); array_init(return_value); zend_hash_copy(Z_ARRVAL_P(return_value), &ce->constants_table, (copy_ctor_func_t) zval_add_ref, (void *) &tmp_copy, sizeof(zval *)); } +/* }}} */ +/* {{{ proto mixed Reflection_Class::getConstant(string name) + Returns the class' constant specified by its name */ ZEND_FUNCTION(reflection_class_getconstant) { - zval *object; reflection_object *intern; zend_class_entry *ce; zval **value; zval **name; int argc = ZEND_NUM_ARGS(); + METHOD_NOTSTATIC; if (zend_get_parameters_ex(argc, &name) == FAILURE) { ZEND_WRONG_PARAM_COUNT(); } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (ce = intern->ptr) == NULL) { - RETURN_FALSE; - } + GET_REFLECTION_OBJECT_PTR(ce); if (zend_hash_find(&ce->constants_table, Z_STRVAL_PP(name), Z_STRLEN_PP(name) + 1, (void **) &value) == FAILURE) { RETURN_FALSE; } *return_value = **value; zval_copy_ctor(return_value); } +/* }}} */ +static void _check_flag(INTERNAL_FUNCTION_PARAMETERS, int mask) +{ + reflection_object *intern; + zend_class_entry *ce; + + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); + RETURN_BOOL(ce->ce_flags & mask); +} + +/* {{{ proto bool Reflection_Class::isInterface() + Returns whether this is an interface or a class */ ZEND_FUNCTION(reflection_class_isinterface) { - zval *object; + _check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_INTERFACE); +} +/* }}} */ + +/* {{{ proto bool Reflection_Class::isFinal() + Returns whether this class is final */ +ZEND_FUNCTION(reflection_class_isfinal) +{ + _check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_FINAL_CLASS); +} +/* }}} */ + +/* {{{ proto bool Reflection_Class::isAbstract() + Returns whether this class is abstract */ +ZEND_FUNCTION(reflection_class_isabstract) +{ + _check_flag(INTERNAL_FUNCTION_PARAM_PASSTHRU, ZEND_ACC_ABSTRACT_CLASS); +} +/* }}} */ + +/* {{{ proto bool Reflection_Class::isInstance(stdclass object) + Returns whether the given object is an instance of this class */ +ZEND_FUNCTION(reflection_class_isinstance) +{ reflection_object *intern; zend_class_entry *ce; + zval *object; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); + METHOD_NOTSTATIC; + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &object) == FAILURE) { + return; } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (ce = intern->ptr) == NULL) { + GET_REFLECTION_OBJECT_PTR(ce); + RETURN_BOOL(ce == Z_OBJCE_P(object)); +} +/* }}} */ + +/* {{{ proto stdclass Reflection_Class::newInstance(mixed* args) + Returns an instance of this class */ +ZEND_FUNCTION(reflection_class_newinstance) +{ + zval *retval_ptr; + reflection_object *intern; + zend_class_entry *ce; + int argc = ZEND_NUM_ARGS(); + + METHOD_NOTSTATIC; + GET_REFLECTION_OBJECT_PTR(ce); + + object_init_ex(return_value, ce); + + /* Run the constructor if there is one */ + if (ce->constructor) { + zval ***params; + + params = safe_emalloc(sizeof(zval **), argc, 0); + if (zend_get_parameters_array_ex(argc, params) == FAILURE) { + efree(params); RETURN_FALSE; + } + + if (fast_call_user_function(EG(function_table), NULL, NULL, + &retval_ptr, argc, params, + 1, NULL, &ce->constructor TSRMLS_CC) == FAILURE) { + efree(params); + zend_error(E_WARNING, "Invokation of %s's constructor failed\n", ce->name); + RETURN_NULL(); + } + if (retval_ptr) { + zval_ptr_dtor(&retval_ptr); + } + efree(params); } - RETURN_BOOL(ce->type & ZEND_ACC_INTERFACE); } +/* }}} */ +/* {{{ proto Reflection_Class[] Reflection_Class::getInterfaces() + Returns an array of interfaces this class implements */ ZEND_FUNCTION(reflection_class_getinterfaces) { - zval *object; reflection_object *intern; zend_class_entry *ce; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern == NULL || (ce = intern->ptr) == NULL) { - RETURN_FALSE; - } + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); + + /* Return an empty array if this class implements no interfaces */ + array_init(return_value); + if (ce->num_interfaces) { int i; - array_init(return_value); + for(i=0; i < ce->num_interfaces; i++) { zval *interface; ALLOC_ZVAL(interface); @@ -600,25 +584,25 @@ } } } +/* }}} */ +/* {{{ proto Reflection_Class Reflection_Class::getParentClass() + Returns the class' parent class, or, if none exists, FALSE */ ZEND_FUNCTION(reflection_class_getparentclass) { - zval *object; reflection_object *intern; zend_class_entry *ce; - if (ZEND_NUM_ARGS() > 0) { - ZEND_WRONG_PARAM_COUNT(); - } - object = getThis(); - intern = (reflection_object *) zend_object_store_get_object(object TSRMLS_CC); - if (intern && (ce = intern->ptr) && ce->parent) { + METHOD_NOTSTATIC_NUMPARAMS(0); + GET_REFLECTION_OBJECT_PTR(ce); + + if (ce->parent) { reflection_class_factory(ce->parent, return_value TSRMLS_CC); - } - else { + } else { RETURN_FALSE; } } +/* }}} */ static zend_function_entry reflection_function_functions[] = { ZEND_FE(reflection_function, NULL) @@ -647,6 +631,10 @@ ZEND_NAMED_FE(getconstant, ZEND_FN(reflection_class_getconstant), NULL) ZEND_NAMED_FE(getinterfaces, ZEND_FN(reflection_class_getinterfaces), NULL) ZEND_NAMED_FE(isinterface, ZEND_FN(reflection_class_isinterface), NULL) + ZEND_NAMED_FE(isabstract, ZEND_FN(reflection_class_isabstract), NULL) + ZEND_NAMED_FE(isfinal, ZEND_FN(reflection_class_isfinal), NULL) + ZEND_NAMED_FE(isinstance, ZEND_FN(reflection_class_isinstance), NULL) + ZEND_NAMED_FE(newinstance, ZEND_FN(reflection_class_newinstance), NULL) ZEND_NAMED_FE(getparentclass, ZEND_FN(reflection_class_getparentclass), NULL) {NULL, NULL, NULL} }; --=-e8syufE3y9w9J5tddI6I Content-Disposition: attachment; filename=reflection.php Content-Type: application/x-php; name=reflection.php Content-Transfer-Encoding: 7bit * $c= Counter::count(); // $c is now 0 * $c= Counter::count(); // $c is now 1 * */ class Counter extends Object implements Serializable { const START= 0; private static $c= Counter::START; /** * Invoke counter * * @access public * @return int */ public static function count() { return self::$c++; } } /** * A final class * */ final class String { // TBI } /** * An abstract class * */ abstract class Storage { // TBI } /** * An abstract class * */ class StorageImpl extends Storage { function __construct() { $a= func_get_args(); printf("%s(%s)\n", __METHOD__, var_export($a, 1)); } } // {{{ main switch ($argv[1]) { case '-f': try { $func= new Reflection_Function($argv[2]); } catch (Exception $e) { var_dump($e); exit(-1); } var_dump($func); printf( "===> The %s function '%s'\n". " declared in %s\n". " lines %d to %d\n", $func->isInternal() ? 'internal' : 'user-defined', $func->getName(), var_export($func->getFileName(), 1), $func->getStartLine(), $func->getEndline() ); printf("---> Documentation:\n %s\n", var_export($func->getDocComment(), 1)); if ($statics= $func->getStaticVariables()) { printf("---> Static variables: %s\n", var_export($statics, 1)); } printf("---> Invokation results in: "); var_dump($func->invoke()); break; case '-i': try { $class= new Reflection_Class($argv[2]); } catch (Exception $e) { var_dump($e); exit(-1); } var_dump($class->isInstance(new String())); break; case '-n': try { $class= new Reflection_Class($argv[2]); } catch (Exception $e) { var_dump($e); exit(-1); } var_dump($class->newInstance(array_slice($argv, 3))); break; case '-c': try { $class= new Reflection_Class($argv[2]); } catch (Exception $e) { var_dump($e); exit(-1); } var_dump($class); printf( "===> The %s%s%s %s '%s' [extends %s]\n". " declared in %s\n". " lines %d to %d\n", $class->isInternal() ? 'internal' : 'user-defined', $class->isAbstract() ? ' abstract' : '', $class->isFinal() ? ' final' : '', $class->isInterface() ? 'interface' : 'class', $class->getName(), var_export($class->getParentClass(), 1), var_export($class->getFileName(), 1), $class->getStartLine(), $class->getEndline() ); printf("---> Documentation:\n %s\n", var_export($class->getDocComment(), 1)); if (!$class->isInterface()) { printf("---> Implements:\n %s\n", var_export($class->getInterfaces(), 1)); } if ($constants= $class->getConstants()) { printf("---> Constants: %s\n", var_export($constants, 1)); } break; } // }}} ?> --=-e8syufE3y9w9J5tddI6I--