具有PHP中调用级联的多个静态方法

问题描述 投票:0回答:1

为了分解代码,我正在基于静态方法做一个更复杂的结构。

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是正确的。

我尝试没有成功:

  1. call_user_func( array( __CLASS__, $function_name ), $children_options, $args, true);

  2. [forward_static_call( array( 'Foo', $function_name ), $children_options, $args, true ); with const NAME = 'Foo'

一瞬间函数调用应该是:

  1. display_options_2_callback
  2. display_checkboxes
  3. display_items_as_list
  4. display_comboboxes
  5. display_items_as_list

有人知道吗?

php oop static-methods
1个回答
0
投票

要磨练,此代码:

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中的某个地方有错字。

© www.soinside.com 2019 - 2024. All rights reserved.