Jon,
What if we don't have control over what was originally written? Example, I'm creating a wordpress widget which their class is structured in a why where it has to extend the WP_Widget class. See below:
You can see my comment in there where I want to put my code which is in the widget function...
class productWidget extends WP_Widget {
/** constructor */
function productWidget() {
parent::WP_Widget(false, $name = 'Products', array('description'=>'Use this widget to display products'));
}
/** @see WP_Widget::widget */
function widget($args, $instance) {
extract( $args );
$title = apply_filters('widget_title', $instance['title']);
print $before_widget;
if ($title)
{
print $before_title . $title . $after_title;
}
//**************************************************************
//**************************************************************
// RIGHT HERE IS WHERE I WANT TO USE FUNCTIONS FROM ANOTHER CLASS
// THAT I CREATED. WHICH THOSE FUNCTIONS CONNECT TO AN EXTERNAL API, ETC.
// Example, I would like to do something like this:
$this->getProducts();
//**************************************************************
//**************************************************************
print $after_widget;
}
/** @see WP_Widget::update */
function update($new_instance, $old_instance) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['display_type'] = strip_tags($new_instance['display_type']);
$instance['product_limit'] = strip_tags($new_instance['product_limit']);
return $instance;
}
/** @see WP_Widget::form */
function form($instance) {
$title = (!empty($instance['title'])) ? (esc_attr($instance['title'])) : ('');
$display_type = (!empty($instance['display_type'])) ? (esc_attr($instance['display_type'])) : ('');
$product_limit = (!empty($instance['product_limit'])) ? (esc_attr($instance['product_limit'])) : ('');
// Title
$input = gen_content('input', '', array('class'=>'widefat', 'id'=>$this->get_field_id('title'), 'name'=>$this->get_field_name('title'), 'type'=>'text', 'value'=>$title));
print gen_content('label', _e('Title:') . $input, array('for'=>$this->get_field_id('title')));
print "<br/>";
print "<br/>";
// Display Type
$options = '';
$options .= ($display_type == 'newest') ? (gen_content('option', 'Newest Products', array('value'=>'newest', 'selected'=>'selected'))) : (gen_content('option', 'Newest Products', array('value'=>'newest')));
$options .= ($display_type == 'popular') ? (gen_content('option', 'Most Popular', array('value'=>'popular', 'selected'=>'selected'))) : (gen_content('option', 'Most Popular', array('value'=>'popular')));
$input = gen_content('select', $options, array('class'=>'widefat', 'id'=>$this->get_field_id('display_type'), 'name'=>$this->get_field_name('display_type')));
print gen_content('label', _e('Display Type:') . $input, array('for'=>$this->get_field_id('display_type')));
print "<br/>";
print "<br/>";
// Limit
$options = '';
for ($i=1; $i<=10; $i++)
{
if ($i == $product_limit) { $options .= gen_content('option', $i, array('value'=>$i, 'selected'=>'selected')); }
else { $options .= gen_content('option', $i, array('value'=>$i)); }
}
$input = gen_content('select', $options, array('class'=>'widefat', 'id'=>$this->get_field_id('product_limit'), 'name'=>$this->get_field_name('product_limit')));
print gen_content('label', _e('Limit:') . $input, array('for'=>$this->get_field_id('product_limit')));
}}
Thoughts? I know I could always add my functions to this class, but I'm using them for other things, and besides that, I do not like to have duplicate functions, that just gets sloppy.
Thanks,
Luke