为了分解代码,我正在基于静态方法做一个更复杂的结构。
class Foo{
...
public static function display_options_callback( array $args ){
$items= [
[
'id' => "featured_image",
'label' => "Activate on featured image"
]
];
self::display_checkboxes( $args, $items);
}
public static function display_options_2_callback( array $args ){
$items = [
0 => [
'id' => "pin_in_lists",
'label' => "Activate pin/unpin in list",
'children' =>
[
'comboboxes' => [
0 => [
'id' => "my-pin-option-1",
'label' => "Pin option 1"
],
1 => [
'id' => "my-pin-option-2",
'label' => "Pin option 2"
],
]
]
];
self::display_checkboxes( $args, $items);
}
public static function display_checkboxes( $args, $items, $is_children = false ){
$callback_callable = function ( ... ){
//doing something
};
self::display_items_as_list( $items, $args, $callback_callable, $is_children );
}
public static function display_comboboxes( $args, $options, $is_children = false ){
$callback_callable = function ( ... ){
//doing something
};
self::display_items_as_list( $items, $args, $callback_callable, $is_children );
}
public static function display_items_as_list(array $items, array $args, callable $function, $is_children ){
echo '<ul>';
foreach ($items as $item) {
echo '<li>';
call_user_func( $function, $item, $args );
if (!empty( $item['children'])) {
$children = $item['children'];
foreach ($children as $children_type => $children_options) {
$function_name = sprintf("display_%s", $children_type);
//make error here !!!
self::$function_name( $children_options, $args, true );
}
}
echo '</li>';
echo '</ul>';
}
}
直到$items
数组具有children
属性,self::$function_name( $children_options, $args, true );
都会出错。
[$function_name
是正确的。
我尝试没有成功:
call_user_func( array( __CLASS__, $function_name ), $children_options, $args, true);
[forward_static_call( array( 'Foo', $function_name ), $children_options, $args, true );
with const NAME = 'Foo'
一瞬间函数调用应该是:
有人知道吗?
要磨练,此代码:
class foo {
public static function a() { echo 'a' . PHP_EOL; }
public static function b() { echo 'b' . PHP_EOL; }
public function __construct() {
foreach(['a', 'b'] as $f) {
self::$f();
}
}
}
new foo();
[就像一个咒语一样工作,所以您可能在$children_type
中的某个地方有错字。