I am trying to use OOP in wordpress. Sample code below. As you can see I have to put global $wpdb in each method. I want to avoid duplicating this line in each method.
Is there a way to set this in constructor or into some protected/private variable so i don't have to put that in every function?
class HouseRepository {
public function functionB() {
global $wpdb;
$results = $wpdb->get_results();
return $results;
}
public function functionA() {
global $wpdb;
$results = $wpdb->get_results();
return $results;
}
}
I tried doing in constructor (and removed from each method) it doesn't work.
function __construct() {
global $wpdb;
}