Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/269.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在PHP中动态构建链式函数调用_Php_Function_Object_Dynamically Generated_Kirby - Fatal编程技术网

在PHP中动态构建链式函数调用

在PHP中动态构建链式函数调用,php,function,object,dynamically-generated,kirby,Php,Function,Object,Dynamically Generated,Kirby,我使用PHP(与KirbyCMS一起使用)并可以创建以下代码: $results = $site->filterBy('a_key', 'a_value')->filterBy('a_key2', 'a_value2'); 这是一个带有两个过滤器的链。它起作用了 但是,我需要动态地构建这样的函数调用。有时可以是两个链式函数调用,有时是三个或更多 这是怎么做到的 也许您可以使用此代码? 链只是一个随机数,可用于创建1-5条链 for( $i = 0; $i < 10; $i +

我使用PHP(与KirbyCMS一起使用)并可以创建以下代码:

$results = $site->filterBy('a_key', 'a_value')->filterBy('a_key2', 'a_value2');
这是一个带有两个过滤器的链。它起作用了

但是,我需要动态地构建这样的函数调用。有时可以是两个链式函数调用,有时是三个或更多

这是怎么做到的

也许您可以使用此代码?

链只是一个随机数,可用于创建1-5条链

for( $i = 0; $i < 10; $i ++ ) {
    $chains = rand(1, 5);
}
示例二,许多嵌套函数调用

$results = $site->filterBy('a_key', 'a_value')->filterBy('a_key2', 'a_value2')->filterBy('a_key3', 'a_value3')->filterBy('a_key4', 'a_value4')->filterBy('a_key5', 'a_value5')->filterBy('a_key6', 'a_value6');
$chains=rand(1,5)
$results=$site
$suffix=''
对于($i=1;$i filterBy('a_键'.$后缀,'a_值'.$后缀)
}

如果您能够将
'a_key 1'
'a_value1'
传递给
过滤器的第一个调用,而不是
'a_key'
'a_value'
,那么您可以通过删除
$suffix
If
块来简化代码,只需添加
$i
,您不需要生成列表您可以将每个调用的参数放入一个列表中,然后编写一个类的新方法,从列表中获取这些参数,并使用它们重复调用
filterBy()

从示例代码中,我假设函数
filterBy()
返回
$this
或与
站点
相同类的另一个对象

//
// The code that generates the filtering parameters:

// Store the arguments of the filtering here
$params = array();

// Put as many sets of arguments you need
// use whatever method suits you best to produce them
$params[] = array('key1', 'value1');
$params[] = array('key2', 'value2');
$params[] = array('key3', 'value3');


//
// Do the multiple filtering
$site = new Site();
$result = $site->filterByMultiple($params);


//
// The code that does the actual filtering
class Site {
    public function filterByMultiple(array $params) {
        $result = $this;
        foreach ($params as list($key, $value)) {
            $result = $result->filterBy($key, $value);
        }
        return $result;
    }
}

如果
filterBy()
返回
$this
,则不需要工作变量
$result
;调用
$this->filterBy()
返回$this;
并删除其他出现的
$result

您可以用代码显示所需输出的示例吗?因此,如果我理解,您希望在链值时多次使用filterBy函数?因此,如果您有链4,您希望使用filterBy 4次?每次使用参数“a_key”。($chain-1)?对Kirby不太熟悉,但将数组传递给
filterBy()
而不是将其链接六次是否更有意义?纯函数无法做到这一点,但方法可以做到这一点,只要确保每次调用都返回
$this
@JensTörnell我可以,但它是重复的:)
$chains = rand(1, 5)
$results = $site
$suffix = ''
for ( $i = 1; $i <= $chains; $i ++) {
    if ($i != 1) {
        $suffix = $i
    }
    $results = $results->filterBy('a_key' . $suffix, 'a_value' . $suffix)
}
//
// The code that generates the filtering parameters:

// Store the arguments of the filtering here
$params = array();

// Put as many sets of arguments you need
// use whatever method suits you best to produce them
$params[] = array('key1', 'value1');
$params[] = array('key2', 'value2');
$params[] = array('key3', 'value3');


//
// Do the multiple filtering
$site = new Site();
$result = $site->filterByMultiple($params);


//
// The code that does the actual filtering
class Site {
    public function filterByMultiple(array $params) {
        $result = $this;
        foreach ($params as list($key, $value)) {
            $result = $result->filterBy($key, $value);
        }
        return $result;
    }
}