PHP Fatal error: Call to a member function on a non-object [Solved]

PHP Fatal error: Call to a member function on a non-object [Solved]

This article delves into the exploration of comprehending the underlying causes and effective remedies for a PHP Error, more specifically, the error message "PHP Fatal error: Call to a member function on a non-object". This particular error is a recurrent occurrence within PHP, often arising when working with classes. Within the confines of this article, I will be providing you with code snippets that replicate scenarios leading to this error and, in turn, provide viable solutions to address it.

If you find yourself at the inception of your journey with the PHP Language or if you are relatively new to it, encountering this error is possible as you navigate through your developmental phases. Furthermore, if you currently find yourself grappling with this issue, rest assured that by the end of this article, you will acquired the knowledge to effectively rectify the aforementioned error and prevent its recurrence in your forthcoming project development.

What is "PHP Fatal error: Call to a member function on a non-object"?

As I mentioned above, the PHP error that says "PHP Fatal error: Call to a member function on a non-object" is a PHP error that typically emerges in the context of dealing with classes. The aforementioned error message has now transformed into "PHP Fatal error: Uncaught Error: Call to undefined method ClassName::methodName()" in the latest versions of PHP. This error is triggered when an attempt is made to access a non-existing method or property of a class. Other than this error, dealing with classes might return a "Fatal Error: Cannot Redeclare class" error which is basically arises due to the naming convention of the classes.

The error is commonly triggered due to the following reasons:

  • Non-existent class method name: The called class method name does not exist.
  • Typographical error in method name: A typo is present when writing the method name within the class definition.
  • Typographical error in method call: A typo is present when attempting to call the method.
  • Non-method or non-object property: The method or property being accessed is not an object or function; it may be an integer, array, or string.

Here's a PHP snippet the simulate the error to occur:

  1. <?php
  2. /**
  3.   * Sample Class Only
  4.   */
  5.  
  6. class Main{
  7. public function foo(){
  8. return "You have access the foo() Object from the Main class";
  9. }
  10. public function bar(){
  11. return "You have access the bar() Object from the Main class";
  12. }
  13. }
  14.  
  15. // defining the class into a variable
  16. $main = new Main();
  17.  
  18. // Output data from class object
  19. echo $main->getMessage();
  20. ?>

PHP Fatal error: Call to a member function on a non-object [Solved]

How to fix the PHP Fatal error: Call to a member function on a non-object ?

In essence, the PHP Fatal error: Call to a member function on a non-object error arises due to the absence of the invoked method or property within a class. Consequently, the straightforward solution for this issue involves ensuring that the called object method is appropriately defined as a property within the class.

  1. <?php
  2. /**
  3.   * Sample Class Only
  4.   */
  5.  
  6. class Main{
  7. public function foo(){
  8. return "You have access the foo() Object from the Main class";
  9. }
  10. public function bar(){
  11. return "You have access the bar() Object from the Main class";
  12. }
  13. public function getMessage(){
  14. return "Sample Message";
  15. }
  16. }
  17.  
  18. // defining the class into a variable
  19. $main = new Main();
  20.  
  21. // Output data from class object
  22. echo $main->getMessage();
  23. ?>

PHP Fatal error: Call to a member function on a non-object [Solved]

Moreover, it's important to emphasize that this aforementioned error can also be proactively addressed, thereby preventing the abrupt termination or immediate crashing of the complete PHP script. PHP comes with numerous built-in functions, one these is the property_exists() function. This function emerges as a highly valuable asset in scenarios like the one we're discussing.

The property_exists() function serves as a valuable PHP tool that facilitates verification or validation regarding the presence of a property name or method name within a specific class. This function necessitates two arguments: the first being is the class where to find the property and the second being is the property name being examined. Embracing this function aligns seamlessly with established best practices in PHP coding, particularly when navigating the complexities associated with classes in a other PHP libraries.

Here is a sample snippet that illustrate the use of the property_exists() function:

  1. <?php
  2. /**
  3.   * Sample Class Only
  4.   */
  5.  
  6. class Main{
  7. public function foo(){
  8. return "You have access the foo() Object from the Main class";
  9. }
  10. public function bar(){
  11. return "You have access the bar() Object from the Main class";
  12. }
  13. }
  14.  
  15. // defining the class into a variable
  16. $main = new Main();
  17. if(property_exists($main, 'getMessage')){
  18. // Output data from class object
  19. echo $main->getMessage();
  20. }else{
  21. // Do something if object does not exists
  22. echo "The given object does not exists";
  23. }
  24.  
  25. ?>

PHP Fatal error: Call to a member function on a non-object [Solved]

Conclusion

The PHP Fatal error: Call to a member function on a non-object or now returned as PHP Fatal error: Uncaught Error: Call to undefined method ClassName::methodName() occurs when dealing with a non-existing method or object in PHP class. This error can be address by making sure that the method is defined correctly in the class. Furthermore, using property_exists() function is the best practice in writing PHP code for dealing with classes to prevent the error to crash of terminate the whole PHP script.

And there you have it! I trust that this article will prove to be a valuable resource, enriching your understanding and proficiency in utilizing the PHP programming language.

You might also want to explore the following PHP errors:

Explore more on this website for more Free Source Codes, Tutorials, and Article for fixing some errors that may occur in your PHP development.

Happy Coding =)

Add new comment