0

Reference: Am using Slim Framework to develop Rest API application. Problem: While doing so, used static functions. However, one of the functions that were called from my api code, threw an error "Using $this when not in object context". Since I cannot change the code being called which is generating the issue, I need to change my code.

Sample Code before change: $app->post($mer_token_endpoint, get_token);

The rest of the code is fine, except the issue of $this. Wherever it is not called, those APIs work fine.

Now I changed the code to:

class Token {
    public function get_token(Request $request, Response $response, array $args) {
.... code ....
    }
}
$TokenObj = new Token();
$app->post($token_endpoint, $TokenObj->get_token);

How do I pass the reference of the function get_token declared in the class Token in the function post ?

user1300830
  • 83
  • 1
  • 10

1 Answers1

0

Your code seems right but i want to know more about your code. Do you use static property in your function because if you have static class or function we use self instead of $this to access static property also without creating object

Shojaeddin
  • 1,851
  • 1
  • 18
  • 16
  • Actually there is only 1 function inside the class. That function in turn includes another file, which we cannot change. Inside THAT file, is the problem "Using $this when not in object context" being generated. – user1300830 Apr 10 '19 at 06:31
  • Did you see this link https://stackoverflow.com/questions/2350937/php-fatal-error-using-this-when-not-in-object-context – Shojaeddin Apr 10 '19 at 06:47
  • Again here this error mention about static and how to use it – Shojaeddin Apr 10 '19 at 06:49