将变量传递给扩展类 [英] Pass variables to extended class

查看:104
本文介绍了将变量传递给扩展类的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I use the same code to create a widget in WordPress. The code I use extends a class already. It works fine.

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

Rather than copy/paste this every time I need a new widget it makes sense to make this so I can call it with a number of parameters and get it to work.

我有4个变量。我不熟悉如何做这个与类。如果这是一个函数,我会像下面这样调用它

I have 4 variables to pass. I'm not familiar with how to do this with classes. If this was a function I would call it like

function function_name($ functionname,$ classname,$ description,$ filename)

function function_name($functionname, $classname, $description, $filename)

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

How can I achieve a similar results when I extend a class. You can see the four variables above. The end result is to create a widget item in WordPress which when loaded adds a file from a specific location.

虽然这是在WordPress中使用我决定发布on stackoverflow而不是wp-stackoverflow,因为这更常见地涉及类。

Although this is used in WordPress I decided to post on stackoverflow and not wp-stackoverflow because this relates to classes more generally.

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");') );


推荐答案

也许你需要这样的东西吗?

Maybe you need something like this?

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' );
});

这篇关于将变量传递给扩展类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆