-1

我是 OOP 的新手并且有一个课程,在这个课程中我正在尝试运行

foreach($categories as $category) { 
                public function beam_channels_setting.$category->name()
                {
                    echo "<input name='beam_plugin_options[beam_channels".$category->name"]' type='text' value='{$this->options['beam_channels.$category->name']}'/>";

                        } 
        }

但是我不断收到此错误:意外的 T_FOREACH

我知道这是因为它正在寻找一个函数,但我不知道如何克服这个问题,我会把它全部放在另一个函数中吗?

班级

Class BEAM_Options {

    public $options;
    public $categorielist;

    public function __construct()
    {
        $args = array(
            'type'                     => 'post',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 0, //<--IMPORTANT!!
            'hierarchical'             => 1,
            'taxonomy'                 => 'category',
            'pad_counts'               => false );
        $categories = get_categories($args);
        $this->options = get_option('beam_plugin_options');
        $this->register_settings_and_fields();
    }

    public function add_menu_page()
    {
        add_options_page('Ad Management', 'Ad Management', 'administrator', __FILE__, array('BEAM_Options', 'display_options_page'));
    }

    public function display_options_page()
    {
        ?>

        <div class="wrap">
            <?php screen_icon(); ?>
            <h2> Ad Management</h2>
            <form method="post" action="options.php">
                <?php settings_fields('beam_plugin_options'); ?>
                <?php do_settings_sections(__FILE__); ?>


                <p class="submit">
                    <input name="submit" type="submit" class="button-primary" value="Save Changes" />
                </p>
            </form>
        </div>
        <?php
    }

    public function register_settings_and_fields()
    {
        register_setting('beam_plugin_options', 'beam_plugin_options');
        add_settings_section('beam_main_section', 'Settings', array($this, 'beam_main_section_cb'), __FILE__); //id, title, section, callback, page
        add_settings_field('beam_refresh_time_truth','Do you want the Advertisements to Auto-Refresh?', array($this,'beam_refresh_time_truth_setting'), __FILE__, 'beam_main_section');
        add_settings_field('beam_refresh_time','Refresh Time(seconds)', array($this,'beam_refresh_time_setting'), __FILE__, 'beam_main_section');
        $args = array(
            'type'                     => 'post',
            'orderby'                  => 'name',
            'order'                    => 'ASC',
            'hide_empty'               => 0, //<--IMPORTANT!!
            'hierarchical'             => 1,
            'taxonomy'                 => 'category',
            'pad_counts'               => false );
            $categories = get_categories($args);
          foreach($categories as $category) { 
        add_settings_field("beam_channels.$category->name","$category->name", array($this,"beam_channels_setting.$category->name"), __FILE__, 'beam_main_section');
        }
    }


          foreach($categories as $category) { 
                public function beam_channels_setting.$category->name()
                {
                    echo "<input name='beam_plugin_options[beam_channels".$category->name"]' type='text' value='{$this->options['beam_channels.$category->name']}'/>";

                        } 
        }
}
4

3 回答 3

2

如果在 a 内,则必须在 a内。foreach() functionclass

public function somefunction(){
 foreach(...){
  ...
 }
}

如果您希望在每次实例化类时调用它,您可以使用__construct().

public function __construct(){
 foreach(...){
  ...
 }
}
于 2012-05-08T20:50:06.243 回答
1
public function beam_channels_setting.$category->name() {..}

不会工作。如果你想声明你的函数,它必须在你的循环之外。

public function functionName(){
     ...
}

然后在你的循环中使用它:

foreach(...){
  $this->functionName();
}

在你的课上。

于 2012-05-08T20:56:04.707 回答
0

是的,foreach 必须在一个函数中。如果希望在构造类时执行它,可以将它放在__construct()函数中。

于 2012-05-08T20:50:23.753 回答