Home >Backend Development >PHP Tutorial >Call to a Member Function on a Non-Object: How to Diagnose and Fix This PHP Error?

Call to a Member Function on a Non-Object: How to Diagnose and Fix This PHP Error?

Susan Sarandon
Susan SarandonOriginal
2024-12-21 22:58:26382browse

Call to a Member Function on a Non-Object: How to Diagnose and Fix This PHP Error?

Call to a Member Function on a Non-Object: Causes and Solutions

Introduction:

When attempting to access a class member function on a variable that is not an instance of an object, PHP generates the "Call to a member function on a non-object" error message. This issue commonly stems from two primary causes.

1. Null or Nonexistent Object:

The most prevalent cause is when a function returns a null or non-existent object, often encountered when querying a database. To resolve this issue, check the result of the database query and only process the object if it was actually fetched.

Example:

<br>$objPage = $db_result->fetch_object();<br>if ($objPage) {</p>
<pre class="brush:php;toolbar:false">$page

The above is the detailed content of Call to a Member Function on a Non-Object: How to Diagnose and Fix This PHP Error?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn