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
Release: 2024-12-21 22:58:26
Original
381 people have browsed it

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>
<div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false">$page
Copy after login

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!

source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template