1

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?

5
  • How do you create BaseContext object? Commented Apr 19, 2017 at 9:32
  • the issue you are using startSuite() statically, so you are never constructing the object. Commented Apr 19, 2017 at 9:34
  • self refers to the class itself, and it can only access static methods and properties, whereas $this refers to a new instantiation of the class (called an Object). An object can access static methods and properties, but a static method CANNOT access object methods. Commented Apr 19, 2017 at 9:45
  • ah well. that sucks then. Gotta do it some other way. Thanks Commented Apr 19, 2017 at 9:49
  • Avoid using anything static unless the class requires a property or method to be independent of any object created from it. Or if each object must share the same properties. Commented Apr 19, 2017 at 9:57

2 Answers 2

3

well, the issue you are using startSuite() statically, so you are never constructing the object.

from the docs :

Constructors and Destructors

PHP 5 allows developers to declare constructor methods for classes. Classes which have a constructor method call this method on each newly-created object, so it is suitable for any initialization that the object may need before it is used.

while in your case you are using static methods :

Static Keyword

Declaring class properties or methods as static makes them accessible without needing an instantiation of the class. A property declared as static cannot be accessed with an instantiated class object (though a static method can).

so to solve this issue , you may easily instantiate your object within your startSuite method it-self

public static function startSuite() {
     self::$reporter = new JsonReporter();
     self::$reporter->startSuite();
}
Sign up to request clarification or add additional context in comments.

Comments

1

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

class BaseContext extends RootContext
{
  static $reporter; // !! => you have a placeholder but no obj

  public function __construct() { // !! => constructor only works when you create an obj   
  self::$reporter = new JsonReporter(); // !! => will be never set
  }

  public static function startSuite() {
     self::$reporter->startSuite(); // !! => you are calling method on empty placeholder
  }
}

Solution

class BaseContext extends RootContext
{
  static $reporter; 

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

  public static function getReporter() {
     if(!isset(self::$reporter)) { // if not yet set
      self::setReporter(); // set one
     }
     return self::$reporter; // return reporter
  }

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.