mercredi 19 avril 2017

PHP problems with using static object

I have a class called BaseContext, and another called JsonReporter. BaseContext needs a JsonReporter object and has to call its methods at various points. The problem is that BaseContext has methods that are and have to remain static, but need to work with the JsonReporter object anyway.

So this is what I did:

class BaseContext extends RootContext
{
  static $reporter;

  public function __construct() {
     self::$reporter = new JsonReporter();
  }

  public static function startSuite() {
     self::$reporter->startSuite();
  }
}

And then in JsonReporter:

class JsonReporter
{
  private $message;

  public function startSuite() {      
    $this->message.="{ \"feature\" : [";
  }
}

Ok, now every time in BaseContext startSuite() is called, I get:

Fatal error: Call to a member function startSuite() on null

I have never worked with self:: before and I'm probably not using it correctly. Is what I'm trying to do possible and how can I get it to work?






Comments
0 Comments

0 commentaires:

Enregistrer un commentaire