Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing function prototypes dynamically in PHP
    primarykey
    data
    text
    <p>I'm writing a construct in PHP where a parser determins which function to call dynamically, kind of like this:</p> <pre><code>// The definition of what to call $function_call_spec = array( "prototype" =&gt; "myFunction", "parameters" =&gt; array( "first_par" =&gt; "Hello", "second_par" =&gt; "World")); // Dispatch $funcPrototype = $function_call_spec["prototype"]; $funcPrototype(); // Here we call function 'myFunction'. </code></pre> <p>This is all fine and dandy. But now comes the next step, passing the parameters, which I don't really know if it's possible the way I want to do it. It never stops amazing me however what script languages can do these days, so here goes:</p> <p>One could pass the parameters to the function like this:</p> <pre><code>// Here we call function 'myFunction' with the array of parameters. $funcPrototype( $function_call_spec["parameters"] ); </code></pre> <p>However, I want to declare 'myFunction' properly with clear arguments etc:</p> <pre><code>function myFunction( $first_par, $second_par ) { } </code></pre> <p>The question then follows - Is there any way to pass parameters to a function dynamically simply by looping through the parameter array?</p> <p>To clarify, I <strong>don't</strong> want to do it like this:</p> <pre><code>$funcPrototype( $function_call_spec["parameters"]["first_par"], $function_call_spec["parameters"]["second_par"] ); </code></pre> <p>Because this requires my code to statically know details about myFunction, which goes against the whole idea.</p> <p>Instead I would want to do it in some way like this maybe:</p> <pre><code>// Special magic PHP function which can be used for invoking functions dynamically InvokeFunction( $funcPrototype, $function_call_spec["parameters"] ); </code></pre> <p>Which then results in myFunction being called and all parameters in the array gets passed to each individual parameter variable in the prototype.</p> <p>Any comments are welcome.</p> <p>Regards.</p> <p>/R</p> <p>PS: None of the code in this post has been tested for typos etc.</p>
    singulars
    1. This table or related slice is empty.
    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. 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