Search code examples
phpprivate

PHP: calling on an instance's private method inside an instance's static class


What I'm trying to do is the following:

class A {

  public function __construct($a) {
    // stuff here
  }

  public static function request() {
    $instance = new self("hi");
    $instance->bye(); // this weirdly only sometimes throws errors
  }

  private function bye() {
    // stuff here
  }

}

A::request();

The line of interest is $instance->bye() - is this allowed within the static context in a way but when called on an instance and inside the same class as the constructor? Or is this not a good practice in general? It's strange that this only throws errors sometimes of calling on a private method with no context.

Any help appreciated!


Solution

  • Turns out this is fine and PHPStorm debugger was creating the issue and screwing up the context hence why the error only happened sometimes which apparently happens on our systems ¯_(ツ)_/¯