I have the code
public static function constructMe() {
if(!$this->_instance instanceof self) {
$this->_instance = new self();
}
return $this->_instance;
}
To instansiate the class, and there is a $_instance variable in the class, but I’m getting the error:
Fatal error: Using $this when not in object context
You need to use
Since you’re in a static scope (you have no
$this)Also, make sure you declare
Also, I don’t know if
new self();worksYou could try
new __CLASS__()or just write the name of the class ..And don’t use an
instanceof, just check withissetorempty(it’s more accurate)And be careful using
!$var instanceof something, always write as!($var instanceof something)because you don’t want to accidentally cast to a boolean.