title: Where to put $wpdb with OOP plugin / widget using and WP_widget extension
body: <p>I'm new to plugin and WP development, so I need some help.</p>
  
  <p>What I'm trying to do is plugin that would use/reuse certain images from media folder and display them in side widget, per certain page, in a way that I like, or using shortcodes within certain page. (so should work on both posts\pages and sidebar widgets).</p>
  
  <p>It should be used multiple times, on many many pages. </p>
  
  <p>What I've decided to do is create my own table in WP database, even though I've read here on post that it is maybe not needed: <a href="https://stackoverflow.com/questions/4050384/wordpress-plugin-development-using-oop">WordPress plugin development using OOP</a></p>
  
  <p>I struggle on several fields. First I had problems to find decent explanation how to create widget that can be used multiple times. OK, I have resolved this using:</p>
  
  <pre><code>class FeatDispWidget extends WP_Widget {...}
  </code></pre>
  
  <p>examples, and it realy works, I can have multiple instances and data is saved do wp_options. </p>
  
  <p>Now, I'm trying to use $wpdb. And from all possible examples i see I have to use global $wpdb or include some php files, or extend wpdb with my own DB class, but what is the best / correct way in OOP approach ?</p>
  
  <p>this is part of my code, constructor and try to call db function that gives me errors all the time. </p>
  
  <pre><code>class FeatDispWidget extends WP_Widget {
  
       private $featdisplayer_table;
       $featdisplayer_table = $wpdb-&gt;prefix . 'featdisplayer';
  
  
  /**
   * Widget setup.
   */
  function FeatDispWidget() {
      /* Widget settings. */
      $widget_ops = array( 'classname' =&gt; 'featdisp', 'description' =&gt; __('Sexy Feature Displayer.', 'featdisp') );
  
      /* Widget control settings. */
      $control_ops = array( 'width' =&gt; 300, 'height' =&gt; 350, 'id_base' =&gt; 'featdisp-widget' );
  
      /* Create the widget. */
      $this-&gt;WP_Widget( 'featdisp-widget', __('Feature Displayer Widget', 'featdisp'), $widget_ops, $control_ops );
  }
  
  
  
  
      function featDispDBsetup (){
          global $wpdb;
          global $featdisplayer_table;
  
  
          if ( $wpdb-&gt;get_var( "show tables like '$featdisplayer_table'" ) != $featdisplayer_table ) {
      $sql = "CREATE TABLE $featdisplayer_table (".
          "sandf_id INT NOT NULL AUTO_INCREMENT, ".
          "type VARCHAR( 30 ) NOT NULL, ".
          "attachid INT NOT NULL, ".
          "setid INT NOT NULL, ".
          "imgpath LONGTEXT NOT NULL, ".
                      "title LONGTEXT NOT NULL, ".
          "desc LONGTEXT, ".
                      "linkto LONGTEXT, ".
                      "altertext LONGTEXT, ".
                      "txtnxttoimg LONGTEXT, ".
                      "sortorder INT, ".
          ")";
  
              require_once( ABSPATH . 'wp-admin/includes/upgrade.php' );
      dbDelta( $sql );
          }
  
      }
  </code></pre>
  
  <p>So my questions:</p>
  
  <p>1) can I extend WP_widget, as I do, and display it on both pages and sidebar ?
  2) do I need to extend WP_widget in order to have widget instantiated several times (once on each page)
  3) If I extend WP_widget, where do I put $wpdb in order to create table in wp database</p>
  
  <p>So, this is it for now, I think I took something to complex for my first widget, but I'm not giving up! :) </p>