在PHP类中的foreach

时间:2012-05-08 20:47:25

标签: php oop

我是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']}'/>";

                        } 
        }
}

3 个答案:

答案 0 :(得分:2)

foreach() 必须function内,如果它在class范围内。

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

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

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

答案 1 :(得分:1)

public function beam_channels_setting.$category->name() {..}

无效。如果你想声明你的函数,它必须超出你的循环。

public function functionName(){
     ...
}

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

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

在你班上。

答案 2 :(得分:0)

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