Skip to main content
< All Topics
Print

Fatal Error: Uncaught Error: Call to a Member Function foo() on null

What This Error Actually Means

This error indicates that the variable you are calling a method on is not an object. You expected an object, but PHP got null.

Fatal error: Uncaught Error: Call to a member function foo() on null


This is not a parse error; it is strictly a runtime error that PHP cannot detect at compile time. PHP doesn’t know what type your variable will hold until execution reaches that line of code. By then, it’s too late, and the crash happens immediately.


Because PHP is a loosely typed language, functions can return objects, arrays, false, or null without requiring you to specify a return type. As a result, null can creep in unless you are absolutely sure what the function returns.

Common Causes

1. No result returned by a Database Query

$profile = $db->query("SELECT * FROM users WHERE id = 78")->fetch();
$profile->getProfile(); // $profile is null if no row matched

PDO will return null on empty and false on any execution failure, depending on the fetch mode. Neither is an object.

2. A function returned null unexpectedly

$order = getOrder($id); // returns null if order doesn't exist
$order->getTotal();     // fatal error

This is the most common use case. The function returns null because there is either a failed condition, a missing record, or a code path that forgot a return statement.

3. A class property was never initialised

class ShoppingCart {
    private $coupon_code; // defaults to null

    public function apply() {
        $this->coupon_code->validate(); // fatal error if setCoupon() was never called
    }
}


PHP class properties default to null unless assigned in a setter or constructor. If your method expects an object, initialize the property properly or guard against null.

4. Dependency injection wasn’t wired up

class OrderService {
    private $mailer;

    public function confirm() {
        $this->mailer->send(); // null if never injected
    }
}


This is common when services are manually instantiated without providing the required dependencies.

How to Fix It

Null check before calling:

if ($user !== null) {
    $user->getProfile();
}

Use the nullsafe operator (PHP 8.0+):

$user?->getProfile();

The nullsafe operator ?-> stops execution of the entire expression if it encounters null at any point instead of throwing a fatal error.

$price = $order?->getCart()?->getTotal() ?? 0;

In chained calls like above, the nullsafe operator allows a clean fallback to 0 without a fatal error.

Enforce return types in PHP 8:

function getOrder(int $id): Order {
    // PHP will throw a TypeError if this returns null
}


Declaring a non-nullable return type forces the function to always return an object and prevents it from returning null.

Debugging It Fast

Add var_dump() immediately before the line causing the fatal error:

var_dump($order);
$order->getTotal();

Trace back to where $order was assigned if it outputs null.

The fix is not on the line throwing the fatal error—it’s wherever the variable was set to null upstream.

PHP tells you the exact file and line number where the crash occurs. Follow the call stack to narrow it down and debug faster.

Was this article helpful?
0 out of 5 stars
5 Stars 0%
4 Stars 0%
3 Stars 0%
2 Stars 0%
1 Stars 0%
5
Please Share Your Feedback
How Can We Improve This Article?
Table of Contents