Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4072 pages
Node.js, PHP and APIs

Call to a member function on null, although the constructor sets the object

ErrorFatal error: Uncaught Error: Call to a member function query() on null

Short answer

The error means the variable in front of the arrow holds null at that moment. When you did set it in a constructor, the usual reasons are that a child class constructor skipped parent::__construct(), that the variable lives in another scope, or that a method returned null. Dump the variable right before the call to see which.

  • Applies to PHP 8.3, PHP 8.4
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Fix it fast

  1. 1Find out which cause you have

    Print the value just before the failing line. If it is null here, work back to where it should have been assigned.

    var_dump($this->db);
    $this->db->query($sql);
  2. 2Match what you see, then jump to its fix

    1. 1The object is created in a parent class, and the child class has its own __construct()The child class constructor never calls the parent constructorFix
    2. 2A global variable is used inside a function or method, and PHP also warns Undefined variableThe variable is from another scope inside a functionFix
    3. 3The failing line chains calls such as $repo->find(5)->nameA method returned null and the result is used straight awayFix

What does the error look like?

  • Fatal error: Uncaught Error: Call to a member function query() on null
  • Call to a member function ... on null

Cause 1 of 3

The child class constructor never calls the parent constructor

1Check

The PHP manual says parent constructors are not called implicitly when the child defines a constructor.

grep -rn "function __construct" src/

2Fix

Call parent::__construct() from the child constructor, with the arguments it needs.

class Child extends Base {
    public function __construct(Db $db) {
        parent::__construct($db);
    }
}

3Verify

Create the child object and call the method again.

php script.php

Confirmed by php.net

Did this fix it?

Cause 2 of 3

The variable is from another scope inside a function

1Check

The manual says variables created outside a function are not visible inside it unless declared global.

grep -n "global " file.php

2Fix

Pass the object in as an argument or store it in a property and use $this. Use global only in legacy code.

function save(Db $db, $row) {
    $db->insert($row);
}

3Verify

Call the function with the object and confirm the error is gone.

php script.php

Confirmed by php.net

Did this fix it?

Cause 3 of 3

A method returned null and the result is used straight away

1Check

A lookup that finds nothing may return null, and the next arrow then fails. The manual notes that null is the expected value in such a case, which is when the nullsafe operator fits.

var_dump($repo->find(5));

2Fix

Handle the null case, or use ?-> where null is an expected result (PHP 8.0 and later).

$user = $repo->find(5);
$name = $user?->name;

3Verify

Run the line with an id that exists and one that does not.

php script.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Use constructor promotion or typed properties so a missing value fails early and clearly.
  • Call parent::__construct() in every child that defines its own constructor.
  • Pass dependencies in as arguments instead of reaching for globals.

Sources

How was this page made?

I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.

Which stack is behind this page?

Projects of mine built on the same technologies:

Related fixes

Back end

Apache returns 403 when a URL contains %3F

a request like /page%3Fname=test is refused while /page?name=test works

Recent Apache httpd blocks a rewrite when the request contains an encoded question mark (%3F) and the rewritten target has a literal ?.

2 causes, each with a fix

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.