Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As far as I know, this is not what <code>ZEND_BEGIN_ARG_INFO_EX</code> is for.</p> <p><code>ZEND_BEGIN_ARG_INFO_EX</code> is a PHP 5 addition is used for producing cleaner code, enabling type hinting, pass-by-reference and reflection. Consider the following arginfo declarations for your actual function that just returns true:</p> <pre><code>ZEND_BEGIN_ARG_INFO_EX(arginfo_test, 0, 0, 3) ZEND_ARG_INFO(0, firstArg) ZEND_ARG_OBJ_INFO(0, objNonNull, stdClass, 0) ZEND_ARG_OBJ_INFO(0, obj, stdClass, 1) ZEND_ARG_OBJ_INFO(1, objByRef, stdClass, 1) ZEND_END_ARG_INFO() </code></pre> <p>It has the following effect:</p> <pre><code>sample_with_types(); // ok sample_with_types(1, null); // error: arg #2 should be stdClass sample_with_types(1, new stdClass, null); // ok sample_with_types(1, new stdClass, 1); // error: arg #3 should be stdClass sample_with_types(1, new stdClass, null, 2); // error: arg #4 must be reference </code></pre> <p>Additionally, it provides reflection capabilities to your function:</p> <pre><code>$ref = new ReflectionFunction('sample_with_types'); var_dump($ref-&gt;getParameters()); </code></pre> <p>...giving output similar to:</p> <pre><code>array(4) { [0]=&gt; &amp;object(ReflectionParameter)#2 (1) { ["name"]=&gt; string(8) "firstArg" } [1]=&gt; &amp;object(ReflectionParameter)#3 (1) { ["name"]=&gt; string(10) "objNonNull" } [2]=&gt; &amp;object(ReflectionParameter)#4 (1) { ["name"]=&gt; string(3) "obj" } [3]=&gt; &amp;object(ReflectionParameter)#5 (1) { ["name"]=&gt; string(8) "objByRef" } } </code></pre> <p>If you omit the arginfo, <code>ReflectionFunction::getParameters()</code> returns an empty array instead.</p> <p>The <code>required_num_args</code> macro parameter is used specifically for reflection, and denotes how many parameters will be marked required when reflecting the function.</p> <p>If you need to make the arguments required and not just mark them as required when using reflection, you still have to use <code>zend_parse_parameters</code>, which in most cases, you will still need to get the actual values of the arguments:</p> <pre><code>PHP_FUNCTION(sample_with_types) { long arg1; zval *arg2 = NULL, *arg3 = NULL, *arg4 = NULL; zend_class_entry ce2, ce3, ce4; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "looo", &amp;arg1, &amp;arg2, &amp;ce2, &amp;arg3, &amp;ce3, &amp;arg4, &amp;ce4) == FAILURE) { return; } RETURN_TRUE; } </code></pre> <p>Note how I used <code>"looo"</code> (generic object types) and not <code>"lOO!O!"</code> (specific object types with null specifiers) in the above. The type hinting already has been specified with arginfo, so there's no need to do it twice.</p> <p>So, without arginfo:</p> <ul> <li>You'd have to use a handful of <code>zend_fetch_class</code> calls and class entries to type hint your object arguments.</li> <li>It wouldn't enable reflection.</li> <li>You wouldn't be able to declare arguments passed by reference.</li> <li>It would obviously produce less clean code.</li> </ul> <p>For obvious reasons, you'll want to make sure both your arginfo declaration and your <code>zend_parse_parameters</code> call matches.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload