如何使用变量方法名

How to use variable method names

我的 Methods 实体中有 5 个方法:

method1
method2
method3
method4
method5

我的目标是用这 5 种方法随机生成 table。 为此,我有一个数字从 1 到 5 的数组,例如

array(1,3,5,4,2);

然后,在 twig 文件中生成我的 table 时,我想调用:

<td>methods.method<array[0]></td>
<td>methods.method<array[1]></td>
....

所以真正调用的方法是

<td>methods.method1></td>
<td>methods.method3></td>
....

有什么想法吗?谢谢。

我喜欢解决方案:使用 attribute

 attribute(methods, 'method'~array[x])

尝试实现新方法:

function callMethod(int $method_number){
    return $this->'method'.$method_number;

}

然后在树枝上:

... {{ object.callMethod(random_number)  }} ...