I am developing an object to interface with an API. Some calls to the API require authentication and others do not. In terms of best practice, should
a) The authentication method returns a token. The method is called from the controller just before a call to an api call/method that needs authentication and the token is passed in as a parameter.
class api {
public function auth() {
.....
return $token;
}
public function getInfo($token) {
.....
}
b) The authentication method sets a property of the class with token and you must remember to call the method before calling a method that needs auth
class api {
private $token;
public function auth() {
.....
$this->token = $token;
}
public function getInfo() {
$token = $this->token;
.....
}
c) Methods that need auth call to auth method themselves
class api {
private $token;
public function auth() {
.....
return $token;
}
public function getInfo() {
$token = $this->auth();
.....
}
I suppose the above could have a cached auth as a property to save unnecessary calls to auth api call.
Or any other way? Just looking for which is the best practice for such an object and why. Thanks in advance!