I have the following code connecting to the AWS PHP SDK in the form of a Wordpress plugin page. I'm receiving the Call to a member function on a non-object
error when trying to use the $MTurk
variable in the last function (bz_page_file_path
). I've attempted various solutions like $global variables, but have had no luck. I've also confirmed that the code does work if I move the contents of bz_page_file_path
into the constructor function (so it appears to be some kind of scoping issue perhaps). What can I do to pass this to the function correctly?
<?php
class BZ_NamePicker {
// Constructor
function __construct() {
require_once dirname(__FILE__) . '/aws-autoloader.php';
$MTurk = new Aws\MTurk\MTurkClient([
...
]);
add_action( 'admin_menu', array( $this, 'bz_add_menu' ));
}
/* Action to load admin menu */
function bz_add_menu() {
add_menu_page('NamePicker', 'NamePicker', 'manage_options', 'namepicker-dashboard',
array(__CLASS__, 'bz_page_file_path'),
'dashicons-sticky','3');
}
/* Action to load content on admin webpage */
function bz_page_file_path() {
$accountBalance = $MTurk->getAccountBalance([]);
echo $accountBalance['AvailableBalance'];
}
}
new BZ_NamePicker();
<?php
class BZ_NamePicker {
// Constructor
function __construct() {
global $MTurk;
require_once dirname(__FILE__) . '/aws-autoloader.php';
$MTurk = new Aws\MTurk\MTurkClient([
...
]);
add_action( 'admin_menu', array( $this, 'bz_add_menu' ));
}
/* Action to load admin menu */
function bz_add_menu() {
add_menu_page('NamePicker', 'NamePicker', 'manage_options', 'namepicker-dashboard',
array(__CLASS__, 'bz_page_file_path'),
'dashicons-sticky','3');
}
/* Action to load content on admin webpage */
function bz_page_file_path() {
global $MTurk;
$accountBalance = $MTurk->getAccountBalance([]);
echo $accountBalance['AvailableBalance'];
}
}
new BZ_NamePicker();
you need to call the class in the function you want to access it to, as a global object