Introduction
This article discusses about a common error – “Error: Call to a member function on null” in PHP code.
Root-cause
Let’s understand why this error occurs. This error is caused when you are trying to invoke a function through an object variable which is actually null or undefined. Simply put, let’s say you define an instance variable or a static variable in a class but don’t initialise it, so it remains null and you try to invoke a function through that variable which will result in this error.
See example –
- First, we create a simple class called MyClass with one variable called `text` and we initialise it in the constructor. This class also has a function called `printText()` which simply prints the text.
class MyClass {
public $text;
public function __construct(){
$this->text = "Techshshila is fun";
}
public function printText(){
echo $this->text;
}
}
- Let’s use the above class in our `ServiceClass` where we declare a variable of the above class but don’t initialise it. Now, when we try to call `callPrintText()`, we find that the above error occurs.
‘Error: Call to a member function printText() on null ‘
class ServiceClass {
public $myClass;
public function callPrintText(){
echo $this->myClass->printText();
}
}
Fix: Make sure the variable is initialised
To fix the above code, simply initialise the variable in constructor if it is an instance variable.
class ServiceClass {
public $myClass;
//------- initialise the instance variable in a constructor
public function __construct(){
$this->myClass = new MyClass();
}
public function callPrintText(){
echo $this->myClass->printText();
}
}
One Common Mistake
Generally, we declare the instance variable in the constructor but when we have static functions, they do not get initialised in the constructor, due to which, this error is more common. See this common loop-hole:
class ServiceClass {
//------- define a static variable
public static $myClass;
//------- LOOP-HOLE : constructor DOES NOT intialise the static variable
public function __construct(){
self::$myClass = new MyClass();
}
//------- 'Error: Call to a member function printText() on null'
public static function callPrintText(){
echo self::$myClass->printText();
}
}
Right way to initialise the static variable in PHP class
When you have a static variable, we need to ensure that it is initialised correctly before it is used anywhere in the class. The correct way to initialise it is shown below –
class ServiceClass {
//------- define a static variable
public static $myClass;
//------- create an initialise function to check if the variable is initialised at most once
public static function initialise(){
if(isset(self::$myClass)){
self::$myClass = new MyClass();
}
}
//------- create a getter function for the variable $myClass
public static function getMyClass(){
self::initialise();
return self::$myClass;
}
//------- works fine !
public static function callPrintText(){
echo self::getMyClass()->printText();
}
}
Hope you have understood this issue and this article helps you fix it as well.
Happy Reading !