# array and call_user_function

- Thread: https://externals.io/message/40348
- Original archive: https://news-web.php.net/php.internals/40348
- Started by mr esnafrao on 2008-09-08T16:58:19+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #40348 — mr esnafrao — 2008-09-08T16:58:19+00:00

URL: https://externals.io/message/40348#40348

Hi all,
I'm having problems passing an array to the function call_user_function as
parameter. This is the code:

    zval *args[3], *func, *retval;

    MAKE_STD_ZVAL(func);
    MAKE_STD_ZVAL(retval);
    MAKE_STD_ZVAL(args[0]);
    MAKE_STD_ZVAL(args[1]);
    MAKE_STD_ZVAL(args[2]);
    ALLOC_HASHTABLE(args[2]);

    // creates hash and fills
    zend_hash_init(args[2], 0, NULL, NULL, 0);
    g_hash_table_iter_init (&iter, m->params);
    while (g_hash_table_iter_next (&iter, &key, &value)) {
        zend_hash_add(args[2], key, strlen(key), value, strlen(value),
NULL);
    }

    ZVAL_STRING(args[0], estrdup(m->sender), 0);
    ZVAL_STRING(args[1], estrdup(m->cmd), 0);
    ZVAL_STRING(func, estrdup(handler), 0);

    if (call_user_function(EG(function_table),
            NULL,
            func,
            retval, 3,
            args TSRMLS_CC) == FAILURE) {
        g_debug("error calling method: %s", handler);
    }

    zval_dtor(retval);
    zval_dtor(func);
    zval_dtor(args[0]);
    zval_dtor(args[1]);
    zend_hash_destroy(args[2]);
    FREE_HASHTABLE(args[2]);

I suppose that the problem is the conversion between the Hash and the zval,
but I don't know how to do it.

Thanks in advance :)
regards

---

## #40377 — Marcus Boerger — 2008-09-08T21:55:04+00:00

URL: https://externals.io/message/40348#40377 · In reply to: #40348

Hello mr,

Monday, September 8, 2008, 6:58:19 PM, you wrote:

> Hi all,
> I'm having problems passing an array to the function call_user_function as
> parameter. This is the code:

>     zval *args[3], *func, *retval;

args maust be *** not **

>     MAKE_STD_ZVAL(func);
>     MAKE_STD_ZVAL(retval);
>     MAKE_STD_ZVAL(args[0]);
>     MAKE_STD_ZVAL(args[1]);
>     MAKE_STD_ZVAL(args[2]);
>     ALLOC_HASHTABLE(args[2]);

>     // creates hash and fills
>     zend_hash_init(args[2], 0, NULL, NULL, 0);
>     g_hash_table_iter_init (&iter, m->params);
>     while (g_hash_table_iter_next (&iter, &key, &value)) {
>         zend_hash_add(args[2], key, strlen(key), value, strlen(value),
> NULL);
>     }

>     ZVAL_STRING(args[0], estrdup(m->sender), 0);
>     ZVAL_STRING(args[1], estrdup(m->cmd), 0);
>     ZVAL_STRING(func, estrdup(handler), 0);

>     if (call_user_function(EG(function_table),
>             NULL,
>             func,
>             retval, 3,
>             args TSRMLS_CC) == FAILURE) {
>         g_debug("error calling method: %s", handler);
>     }

>     zval_dtor(retval);
>     zval_dtor(func);
>     zval_dtor(args[0]);
>     zval_dtor(args[1]);
>     zend_hash_destroy(args[2]);
>     FREE_HASHTABLE(args[2]);
args[2] is wrong and anyway all three need to be zval_ptr_dtor()

marcus

> I suppose that the problem is the conversion between the Hash and the zval,
> but I don't know how to do it.

> Thanks in advance :)
> regards



Best regards,
 Marcus

---

## #40431 — mr esnafrao — 2008-09-10T09:51:02+00:00

URL: https://externals.io/message/40348#40431 · In reply to: #40377

Thanks for your response, but I'm unable to make it work. I have changed the
code, and it works with strings and even with arrays, but not with hashes:
Am I missing something?

  zval *hash;
  zval **args[2], *func, *retval;
  zval *str;

  MAKE_STD_ZVAL(retval);
  MAKE_STD_ZVAL(func);
  ZVAL_STRING(func, estrdup(handler), 0);

  MAKE_STD_ZVAL(str);
  ZVAL_STRING(str, estrdup(m->sender), 0);

  MAKE_STD_ZVAL(hash);
  ALLOC_HASHTABLE(hash);
  zend_hash_init(hash, 0, NULL, NULL, 1);
  g_hash_table_iter_init (&iter, m->params);
  while (g_hash_table_iter_next (&iter, &key, &value)) {
      zend_hash_add(hash,
           g_strdup(key),
            strlen(key),
            g_strdup(value),
            strlen(value),
            NULL);
  }

  args[0] = &hash;
  args[1] = &str;
  if (call_user_function_ex(EG(function_table),
            NULL,
            func,
            retval,
            2,
            &args,
            0,
            `NULL` TSRMLS_CC) == FAILURE) {
                      g_debug("error calling method: %s", handler);
  }
  ...
Thanks a lot!
AntP

2008/9/8 Marcus Boerger <helly@php.net>

> Hello mr,
>
> Monday, September 8, 2008, 6:58:19 PM, you wrote:
>
> > […]
> as
> > […]
>
> > […]
>
> args maust be *** not **
>
> > […]
>
> > […]
>
> > […]
>
> > […]
>
> > […]
> args[2] is wrong and anyway all three need to be zval_ptr_dtor()
>
> marcus
>
> > […]
> zval,
> > […]
>
> > […]
>
>
>
> Best regards,
>  Marcus

---

## #40433 — mr esnafrao — 2008-09-10T10:52:38+00:00

URL: https://externals.io/message/40348#40433 · In reply to: #40431

I have solved the problem. I had confused the functions hash_* with the
funtions array_*. This works:

    MAKE_STD_ZVAL(hash);
    array_init(hash);
    g_hash_table_iter_init (&iter, m->params);
    while (g_hash_table_iter_next (&iter, &key, &value)) {
        add_assoc_string(hash, key, value, 0);
    }

Thanks for your help and your patience :)
AntP


2008/9/10 mr esnafrao <esnafrao@gmail.com>

> Thanks for your response, but I'm unable to make it work. I have changed
> the code, and it works with strings and even with arrays, but not with
> hashes: Am I missing something?
>
>   zval *hash;
>   zval **args[2], *func, *retval;
>   zval *str;
>
>   MAKE_STD_ZVAL(retval);
>   MAKE_STD_ZVAL(func);
>   ZVAL_STRING(func, estrdup(handler), 0);
>
>   MAKE_STD_ZVAL(str);
>   ZVAL_STRING(str, estrdup(m->sender), 0);
>
>   MAKE_STD_ZVAL(hash);
>   ALLOC_HASHTABLE(hash);
>   zend_hash_init(hash, 0, NULL, NULL, 1);
>   g_hash_table_iter_init (&iter, m->params);
>   while (g_hash_table_iter_next (&iter, &key, &value)) {
>       zend_hash_add(hash,
>            g_strdup(key),
>             strlen(key),
>             g_strdup(value),
>             strlen(value),
>             NULL);
>   }
>
>   args[0] = &hash;
>   args[1] = &str;
>   if (call_user_function_ex(EG(function_table),
>             NULL,
>             func,
>             retval,
>             2,
>             &args,
>             0,
>             `NULL` TSRMLS_CC) == FAILURE) {
>                       g_debug("error calling method: %s", handler);
>   }
>   ...
> Thanks a lot!
> AntP
>
> 2008/9/8 Marcus Boerger <helly@php.net>
>
> > […]
