i got error Undefined class constant 'app_id'
.I am trying to access the variable declared InitialSetUp class inside getFBInstance()
method and this method is called from static method check_user()
.I guess self keyword in this new Facebook\\Facebook([])
refers to the Facebook class thats why the error occurs but how to access the app_id and other variable in getFBInstance()
require_once __DIR__ . '/vendorFacebook/autoload.php';
class InitialSetUp{
public $app_id = "XXXXXXXXXX";
public $app_secret = "XXXXXXXXXX";
public $default_graph_version = 'v2.11';
}
public function getFBInstance() {
return new Facebook\Facebook([
'app_id' => self::app_id, // here i am not able to access InitialSetUp' app id
'app_secret' => self::app_secret,
'default_graph_version' => self::default_graph_version,
]);
}
public static function check_user() {
$fb = self::getFBInstance();
}
Since $app_id
is not static you need to instantiate the IntialSetUp
class. I would also advise you to look into creating get functions .
public function getFBInstance() {
$initialSetup = new InitialSetUp();
return new Facebook\Facebook([
'app_id' => $initialSetup->app_id, // here i am not able to access InitialSetUp' app id
'app_secret' => $initialSetup->app_secret,
'default_graph_version' => $initialSetup->default_graph_version,
]);
}
The self
keyword refers to elements of the class where it is used and for static properties and methods.
public function getFBInstance() {
return new Facebook\Facebook([
'app_id' => $this->app_id, // here i am not able to access InitialSetUp' app id
'app_secret' => $this->app_secret,
'default_graph_version' => $this->default_graph_version,
]);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.