Reputation: 5429
This is how my code looks like:
public function __construct() {
global $wpdb;
}
private function get_pagination() {
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM yc_customers WHERE $this->get_where" );
}
When I run it, I'll get this error:
Fatal error: Call to a member function get_var() on a non-object
When I copy global $wpdb;
to my get_pagination()
function, then I don't get any errors. I don't want to copy it in all my functions though. Why am I getting this error, even when I have the global $wpdb
in the __construct
function?
Upvotes: 0
Views: 34
Reputation: 36954
If you want use global
, and you don't want it, then you can do something like:
private function get_pagination() {
global $wpdb;
$user_count = $wpdb->get_var( "SELECT COUNT(*) FROM yc_customers WHERE $this->get_where" );
}
But you can simply pass the variable in the constructor, like:
public function __construct($wpdb) {
$this->wpdb = $wpdb;
}
private function get_pagination() {
$user_count = $this->wpdb->get_var( "SELECT COUNT(*) FROM yc_customers WHERE $this->get_where" );
}
Look for "dependency injection".
Upvotes: 1