[wp-hackers] Using wpdb by inheriting the class

Ryan Boren ryan at boren.nu
Sat Jul 31 15:40:23 UTC 2004


On Sat, 2004-07-31 at 03:25 -0600, Chris Coggburn wrote:
> I was talking with a C++ developer, and they sure are fond of OOP 
> programming, and they mentioned that the best way to access $wpdb would 
> be to define your own class for your plugin and then just inherit wpdb 
> into that class. This would allow you to add your own $tables to the 
> $wpdb class. Just wondering what the developers think of this, it has 
> saved me so much time and hack jobs so I am fond of it.
> 
> Example:
> 
> class someplugin extends wpdb {
>   var $newtablevar;
>   function someplugin() { // This calls the wpdb constructor to select 
> the db for access
>   global $loginsql, $passsql, $base, $server, $table_prefix;
>   $this->newtablevar = $table_prefix . 'NEWTABLENAME';
>   $this->wpdb($loginsql, $passsql, $base, $server);
>   }
>   function dosomething() {
>     echo $this->get_row('SELECT 'stuff' FROM $this->newtablevar WHERE id=1);
>   }
> }

It would be easier to just use the global $wpdb instance.  It is already
connected to the DB.  Adding extra code and making another DB connection
just to be able to use $this rather than $wpdb doesn't seem worthwhile.
There's nothing in the sample above that requires an is-a relationship
rather than a simple uses-a.  But, to each plugin author their own.
Although, we really don't need every plugin making separate DB
connections.

Ryan




More information about the hackers mailing list