"Call to a member function X() on null" in PHP — What It Means and How to Fix It

What the PHP error "Call to a member function X() on null" means and how to fix it.

Published October 31, 2026

"Call to a member function X() on null" occurs when a method is called on a variable that is null instead of an object.

Key points

  • Check the exact file and line number PHP reports — the mistake is often on or just before that line

Example

$user = null;
$user->getName();  // Error: Call to a member function getName() on null
More PHP articles