[PHP-DEV] call_user_method_array From: Wez Furlong (wez <email protected>)
Date: 02/25/01

Hi,

I have written a call_user_method_array function that,
surprise surprise, calls a user method and passes arguments from an array - the object/class equivalent of call_user_func_array.

The code is below. In the function table, I have made the second arg passed by reference, as it is not very useful otherwise (a shortcoming of call_user_method).

I have CVS access and will add this code to basic_functions.c sometime tonight (in a couple of hours), unless anyone objects!

--Wez.

/* {{{ proto mixed call_user_method_array(string function_name, object object, array parameters)
   Call a user on a specific object where the first argument is the method name, the second argument is the object, and the arguments contained in array */
PHP_FUNCTION(call_user_method_array)
{
    zval **func_name,
       **object,
         **params,
         ***func_args = NULL,
         *retval_ptr;
    HashTable *params_ar;
    int num_elems,
        element = 0;
   CLS_FETCH();

    if (ZEND_NUM_ARGS() != 3 ||
        zend_get_parameters_ex(3, &func_name, &object, &params) == FAILURE) {
        WRONG_PARAM_COUNT;
    }
   if (Z_TYPE_PP(object) != IS_OBJECT) {
      php_error(E_WARNING,"2nd argument is not an object\n");
      efree(params);
      RETURN_FALSE;
   }

    convert_to_string_ex(func_name);

    params_ar = HASH_OF(*params);
    num_elems = zend_hash_num_elements(params_ar);

    func_args = (zval ***)emalloc(sizeof(zval **) * num_elems);

    for (zend_hash_internal_pointer_reset(params_ar);
         zend_hash_get_current_data(params_ar, (void **)&(func_args[element])) == SUCCESS;
         zend_hash_move_forward(params_ar))
         element++;

    if (call_user_function_ex(CG(function_table), object, *func_name, &retval_ptr, num_elems, func_args, 1, NULL) == SUCCESS
        && retval_ptr) {
        COPY_PZVAL_TO_ZVAL(*return_value, retval_ptr);
    } else {
        php_error(E_WARNING, "Unable to call %s() - function does not exist", Z_STRVAL_PP(func_name));
    }
    efree(func_args);
}
/* }}} */

-- 
PHP Development Mailing List <http://www.php.net/>
To unsubscribe, e-mail: php-dev-unsubscribe <email protected>
For additional commands, e-mail: php-dev-help <email protected>
To contact the list administrators, e-mail: php-list-admin <email protected>