We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Custom function variables

Hi everyone!

I try to add a custom function ti use it in Volt. I've got this thing i hardly understand. If i dump my variables, it has two simple quote before and after for a string, the type is an array for an integer... Here is a simple exemple :

 $compiler->addFunction('test', function($string,$length) { 

                 var_dump($string);
                 var_dump($length);

                 exit;

                 });

And here is the the call in Volt :

    {{ test ("test","20") }}

And the result is :

    string ''test', '20'' (length=20)

array (size=2)
0 => 
array (size=3)
'expr' => 
array (size=4)
'type' => int 260
'value' => string 'test et test' (length=12)
'file' => string '/home/alex/......' (length=69)
'line' => int 1
'file' => string '/home/alex/.....' (length=69)
'line' => int 1
1 => 
array (size=3)
'expr' => 
array (size=4)
'type' => int 260
'value' => string '20' (length=2)
'file' => string '/home/alex/.....' (length=69)
'line' => int 1
'file' => string '/home/alex/.....' (length=69)
'line' => int 1 **

For sure there is something i can't find in documentation about how passing variable to a custom function!



33.8k
edited Jan '15

It is correct as you see; $string pass then as a string, and $length as an multidimensional array. To use a function that uses arguments, do something like this:

$compiler->addFunction('test', function($string) {
    // This will call MyClass::myMethod('test', '20').
    // I didn't add '$length' to the arguments of this clossure because it isn't necessary (at least for this purpose).
    return (
        'MyClass::myMethod(' . $string . ')'
    );
});

I would give you a code that I've to automatize the creation of that type of functions, but it'll be best to understand this first.