【问题标题】:Pass variables to extended class将变量传递给扩展类
【发布时间】:2015-06-08 15:20:53
【问题描述】:

我使用相同的代码在 WordPress 中创建小部件。我使用的代码已经扩展了一个类。它工作正常。

不是每次我需要一个新的小部件时都复制/粘贴它,而是这样做是有意义的,这样我就可以用一些参数调用它并让它工作。

我有 4 个变量要传递。我不熟悉如何在课堂上做到这一点。如果这是一个函数,我会这样称呼它

函数function_name($functionname, $classname, $description, $filename)

我如何在扩展类时获得类似的结果。您可以看到上面的四个变量。最终结果是在 WordPress 中创建一个小部件项目,加载时会从特定位置添加一个文件。

虽然这是在 WordPress 中使用的,但我决定在 stackoverflow 而不是 wp-stackoverflow 上发布,因为这与更普遍的类有关

class exampleWidget extends WP_Widget
{
  function exampleWidget()
  {
    $widget_ops = array('classname' => 'exampleWidget', 'description' => 'Description here...' );
    $this->WP_Widget('exampleWidget', 'Description - Adds Switch', $widget_ops);
  }

  function form($instance)
  {
    $instance = wp_parse_args( (array) $instance, array( 'title' => '' ) );
    $title = $instance['title'];
?>
  <p><label for="<?php echo $this->get_field_id('title'); ?>">Title: <input class="widefat" id="<?php echo $this->get_field_id('title'); ?>" name="<?php echo $this->get_field_name('title'); ?>" type="text" value="<?php echo attribute_escape($title); ?>" /></label></p>
<?php
  }

  function update($new_instance, $old_instance)
  {
    $instance = $old_instance;
    $instance['title'] = $new_instance['title'];
    return $instance;
  }

  function widget($args, $instance)
  {
    extract($args, EXTR_SKIP);

    echo $before_widget;
    $title = empty($instance['title']) ? ' ' : apply_filters('widget_title', $instance['title']);

    if (!empty($title))
      echo $before_title . $title . $after_title;;

    // widget code here
    get_template_part( 'includes/widget/widget-name' );


    echo $after_widget;
  }

}




add_action( 'widgets_init', create_function('', 'return register_widget("exampleWidget");') );

【问题讨论】:

  • 有很多方法可以做到这一点:通过构造传递它们,使用设置器,链方法,你甚至可以定义一些你想要的类属性(虽然不是真的推荐)。选择你的毒药。
  • 谢谢,我会调查的。如果您可以提供一个简单方法的示例,那将是理想的。值得一提的是,我想多次添加它,因此我正在寻找一种优雅且最小的调用方式。蛋。就像我给出的函数 function_name... 示例一样。例如,我想有 5 次并使用发送过来的参数加载类。

标签: php wordpress class oop


【解决方案1】:

也许你需要这样的东西?

class My_WP_Widget extends WP_Widget
{
    protected $id_base = null; // Will generated automatically in WP_Widget constructor
    protected $classname = null; // Will generated automatically in WP_Widget constructor
    protected $name = 'Default widget name';
    protected $description = 'Default widget description';
    protected $filename;

    public function __construct() {
        $widget_options = array(
            'classname' => $this->classname, 
            'description' => $this->description,
            'filename' => $this->filename
        );

        parent::__construct($this->id_base, $this->name, $widget_options);
    }
}

class FilenameOverriding_My_WP_Widget extends My_WP_Widget
{
    protected $filename = 'overriden/filename.ext';
}

// Usage:
add_action( 'widgets_init', function(){
    return register_widget( 'FilenameOverriding_My_WP_Widget' );
});

【讨论】:

  • @raison 你能写用例吗?
  • 不走运.. 出现错误。我原始帖子中的代码显示了我当前如何添加小部件。我想找到一种替代方法,使我可以编写更少的代码。我只解析 ID、文件名、描述和名称的版本。有了这 4 个变量,它将创建一个简单的小部件来使用..
猜你喜欢
  • 2012-04-16
  • 1970-01-01
  • 1970-01-01
  • 2020-09-14
  • 2013-08-16
  • 1970-01-01
  • 2018-12-05
  • 1970-01-01
相关资源
最近更新 更多