Building chained function calls dynamically in PHP -
i use php (with kirbycms) , can create code:
$results = $site->filterby('a_key', 'a_value')->filterby('a_key2', 'a_value2'); this chain 2 filterby. works.
however need build function call dynamically. can 2 chained function calls, 3 or more.
how done?
maybe can play code?
chain random number can used create between 1-5 chains.
for( $i = 0; $i < 10; $i ++ ) { $chains = rand(1, 5); } examples of desired result
example one, 1 function call
$results = $site->filterby('a_key', 'a_value'); example two, many nested function calls
$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 <= $chains; $i ++) { if ($i != 1) { $suffix = $i } $results = $results->filterby('a_key' . $suffix, 'a_value' . $suffix) } if able pass 'a_key1' , 'a_value1' first call filterby instead of 'a_key' , 'a_value', simplify code removing $suffix , if block , appending $i.
Comments
Post a Comment