Fatal error: Uncaught Error: Using $this when not in object context [Solved]

Fatal error: Uncaught Error: Using $this when not in object context [Solved]

This article delves into an exploration of the causes and solutions for the PHP Fatal Error that reads 'PHP Fatal error: Uncaught Error: Using $this when not in object context'. If you are currently encountering this error during the development phase of your PHP project, this article aims to provide you with insights into understanding the error and guidance on how to prevent and resolve it.

What is PHP Fatal Error means?

The PHP programming language encompasses various types of errors that can occur when writing and executing code in this language. One of these error categories is known as Fatal Error. Fatal Errors arise from attempting to invoke non-existing functions, variables, or methods, and they can lead to the termination or crashing of the program. In addition to the Fatal Error message that we are investigating in this article, errors can be thrown such as the following:

Why does the PHP Fatal error: Uncaught Error: Using $this when not in object context occurs?

In PHP, the error message PHP Fatal error: Uncaught Error: Using $this when not in object context is triggered when the $this variable, which serves as a special variable referring to the current object, is utilized outside the scope of an object or class. This error can also occur when attempting to access the $this variable within a static function of an object or class, which is not permitted. In PHP classes, we employ $this to refer to the current object within non-static members. If you need to reference static members, you should use self instead of $this.

How to Fix PHP Fatal error: Uncaught Error: Using $this when not in object context?

This PHP Fatal error can be resolved by ensuring that when you use the $this variable, your code or script must be located within the context of an object or class.

Scenario #1:

Here's a sample snippet that simulate the error to occur.

  1. <?php
  2. class MyClass{
  3.     // Defining Sample Protected Variable
  4.     protected $var1;
  5.     // defining a default valur to the variable
  6.     public function __construct(){
  7.         $this->var1 = "Lorem Ipsum";
  8.     }
  9.     // static class object that attempts to use $this
  10.     public static function get_variable(){
  11.         echo $this->var1;
  12.     }
  13. }
  14. // Initialize class
  15. $myClass = new MyClass();
  16. // Output variable
  17. print_r($myClass::get_variable());
  18. ?>

In the given code snippet, it's evident that we are making an attempt to retrieve or display the var1 property of the MyClass class. Running this script will result in a Fatal Error since we are accessing the var1 variable using $this within the static property or method of the class.

Using $this when not in object context

Solution 1

In the scenario presented above, we can resolve the error by converting the variable in question into a static one and accessing it using self. To gain a clearer understanding, refer to the following code snippet:

  1. <?php
  2. class MyClass{
  3.     // Defining Sample Protected Variable
  4.     static protected $var1;
  5.     // defining a default valur to the variable
  6.     public function __construct(){
  7.         self::$var1 = "Lorem Ipsum";
  8.     }
  9.     // static class object that attempts to use $this
  10.     public static function get_variable(){
  11.         echo self::$var1;
  12.     }
  13. }
  14. // Initialize class
  15. $myClass = new MyClass();
  16. // Output variable
  17. print_r($myClass::get_variable());
  18. ?>

Using $this when not in object context

Solution 2

Furthermore, the initial scenario can be rectified by transforming the static method into a non-static member method of the class. Please refer to the following code snippet for a step-by-step guide on how to achieve this:

  1. <?php
  2. class MyClass{
  3.     // Defining Sample Protected Variable
  4.     protected $var1;
  5.     // defining a default valur to the variable
  6.     public function __construct(){
  7.         $this->var1 = "Lorem Ipsum";
  8.     }
  9.     // non-static class object that attempts to use $this
  10.     public function get_variable(){
  11.         echo $this->var1;
  12.     }
  13. }
  14. // Initialize class
  15. $myClass = new MyClass();
  16. // Output variable
  17. print_r($myClass->get_variable());
  18. ?>

Using $this when not in object context

Scenario #2

Another cause for the Fatal error: Uncaught Error: Using $this when not in object context to occur is attempting to use the $this special variable outside of the object. Please review the provided code snippet for this specific scenario:

  1. <?php
  2.  
  3. class MyClass{
  4.     protected $var1;
  5.     public function __construct(){
  6.         $this->var1 = "Lorem Ipsum";
  7.     }
  8. }
  9. $myClass = new MyClass();
  10. function get_variable(){
  11.     global $myClass;
  12.     return $this->var1;
  13. }
  14.  
  15. // Output variable
  16. print_r(get_variable());
  17. ?>

Using $this when not in object context

Solution 1

In the second scenario, the error can be resolved by utilizing the defined class variable name instead of the $this variable, especially since the get_variable() function is not a member method of any object. Please refer to the PHP code snippet below for a clearer understanding:

  1. <?php
  2.  
  3. class MyClass{
  4.     public $var1;
  5.     public function __construct(){
  6.         $this->var1 = "Lorem Ipsum";
  7.     }
  8. }
  9. $myClass = new MyClass();
  10.  
  11. function get_variable(){
  12.     global $myClass;
  13.     return $myClass->var1;
  14. }
  15.  
  16. // Output variable
  17. print_r(get_variable());
  18. ?>

Using $this when not in object context

Solution 2

Moreover, extending the MyClass into another class or object and moving the get_variable() method to this object is another solution to the problem. In this scenario, the $this variable becomes usable, allowing access to the value of the var1 variable outside the main class.

  1. <?php
  2.  
  3. class MyClass{
  4.     public $var1;
  5.     public function __construct(){
  6.         $this->var1 = "Lorem Ipsum";
  7.     }
  8.     function get_variable(){
  9.         //
  10.     }
  11. }
  12.  
  13. class NextClass extends MyClass{
  14.     public function get_variable(){
  15.         return $this->var1;
  16.     }
  17. }
  18.  
  19. $NextClass = new NextClass();
  20.  
  21.  
  22. // Output variable
  23. print_r($NextClass->get_variable());
  24. ?>

Using $this when not in object context

Conclusion

In simpler terms, the Fatal error: Uncaught Error: Using $this when not in object context error message in PHP occurs when we try to utilize the $this (a special variable in PHP) outside of an object or class. This error can also happen when we employ this variable within a static member method or variable of the current object or class. The solution to this error lies in ensuring that we avoid the causes that lead to its occurrence.

And there you have it! I trust that this article on rectifying the 'Fatal error: Uncaught Error: Using $this when not in object context' will be beneficial for your current situation during your PHP project development phase.

Explore more on this webstire for more Free Source Codes, Tutorials, Articles about programming languages.

Happy Coding =)

Add new comment