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

How to easily retrieve custom function arguments

Hi all,

I'm writing a custom function to add to my Volt compiler. This function is called like this:

{{ icon("times",["style":"solid"]) }}

The second argument is an array that can have a number of different properties defined. Accessing the first argument is pretty straightforward with

$expr[0]['expr']['value']

However, the second parameter appears to be broken up into a complex abstract syntax tree. Here's the exprArgs:

Array
(
    [0] => Array
        (
            [expr] => Array
                (
                    [type] => 260
                    [value] => pencil
                    [file] => /path/to/file
                    [line] => 88
                )

            [file] => /path/to/file
            [line] => 88
        )

    [1] => Array
        (
            [expr] => Array
                (
                    [type] => 360
                    [left] => Array
                        (
                            [0] => Array
                                (
                                    [expr] => Array
                                        (
                                            [type] => 260
                                            [value] => solid
                                            [file] => /path/to/file
                                            [line] => 88
                                        )

                                    [name] => style
                                    [file] => /path/to/file
                                    [line] => 88
                                )

                        )

                    [file] => /path/to/file
                    [line] => 88
                )

            [file] => /path/to/file
            [line] => 88
        )
)

Now, I can still access that with

$expr[0]['expr']['left']['0']['expr']['value']

but that seems kind of brittle and excessive.

Is there any way to simply get the arguments as they were passed?

I'm running v3.4



8.4k
Accepted
answer

credit goes to Nikolay Mihaylov

$compiler->addFunction(
    'icon',
    function ($resolvedArgs, $exprArgs) {
        return 'Helper::something(' . $resolvedArgs . ')';
    }
);

Helper.php:

<?php

class Helper
{
    public static function something($arg1, $arg2)
    {
        var_dump($arg1, $arg2);
    }
}

I was hoping to avoid setting up an entirely separate class, but that's been the only suggestion.

Thanks for the tip.