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?
BaseContextobject?startSuite()statically, so you are never constructing the object.selfrefers to the class itself, and it can only access static methods and properties, whereas$thisrefers to anewinstantiation of the class (called an Object). An object can access static methods and properties, but a static method CANNOT access object methods.