skip to Main Content

In SaxonC PHP extension (written in C++) I am writing utility to allow users to write custom functions in their PHP script which will get called in XSLT, XPath or XQuery. Therefore we have to handle a number of PHP native return types from the user function. This I can do without problem, except for ArrayIterators. We get back a zval which can be an ArrayIterator. But how can I check if it is actually an ArrayIterator type in the PHP internals using the Zend framework written in C++ code?
See the coe snippet below:

zval retval;
int value = (int)Z_TYPE_P(&retval)

The value gives zero (i.e. IS_UNDEF) when I know its should be an ArrayIterator and not IS_OBJECT. How can I check for ArrayIterator?

2

Answers


  1. Z_TYPE(retval) == IS_OBJECT &&
    instanceof_function(Z_OBJCE(retval), spl_ce_ArrayIterator)
    

    spl_ce_ArrayIterator is declared in spl_array.h

    Login or Signup to reply.
  2. You probably should check for Traversable objects in general using instanceof_function(Z_OBJCE_P(data), zend_ce_traversable).

    If the type of the retval is IS_UNDEF after calling a user function, this means the function threw an exception.

    Aside: you can just use Z_TYPE(retval) instead of Z_TYPE_P(&retval)

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search