Maaz Zindani

Loading the index…

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

Fatal error: Cannot access parent:: when current class scope has no parent

ErrorFatal error: Uncaught Error: Cannot access parent:: when current class scope has no parent

Short answer

PHP hit a parent:: call inside a class that does not extend anything. The call is wrong for that class: either the extends clause is missing, or the code was copied from a child class. Add the right extends, or delete the parent:: call. In a trait, the using class must extend a parent.

  • 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

    Read the file and line in the error, then list the class declaration and every parent:: use in that file. The class that holds the parent:: call must have an extends clause.

    grep -n -E "^\s*(abstract |final )?(class|trait) |parent::" src/Service/Mailer.php
  2. 2Match what you see, then jump to its fix

    1. 1The failing line is parent::__construct() or parent::someMethod() inside a class declared as 'class Name' with nothing after itThe class has no extends clauseFix
    2. 2The failing line is inside a trait file, and the class that uses the trait has no extendsA trait calls parent:: but the class that uses it has no parentFix

What does the error look like?

  • Fatal error: Uncaught Error: Cannot access parent:: when current class scope has no parent
  • Deprecated: Cannot use "parent" when current class scope has no parent

Cause 1 of 2

The class has no extends clause

1Check

Look at the class declaration. The manual says parent works from inside a class definition, and its examples always use it in a class that extends another one. In a class without a parent there is nothing to point at. The manual says the error is generated when the parent is accessed at run time.

grep -n "class Mailer" src/Service/Mailer.php
php -l src/Service/Mailer.php

2Fix

If the class should inherit from a base class, add extends. If it should not, remove the parent:: call (a constructor with no parent needs no parent::__construct()).

// before
class Mailer
{
    public function __construct() { parent::__construct(); }
}

// after: the class really has a base class
class Mailer extends BaseService
{
    public function __construct() { parent::__construct(); }
}

3Verify

Run the code path that creates the object. The error no longer appears.

php -r 'require "vendor/autoload.php"; new App\Service\Mailer();'

Confirmed by php.net

Did this fix it?

Cause 2 of 2

A trait calls parent:: but the class that uses it has no parent

1Check

Trait methods are resolved against the class that uses the trait. The manual's trait example calls parent::sayHello() from a trait and works only because the using class extends Base. Find which classes use the trait.

grep -rn "use LogsActivity;" src/
grep -rn "trait LogsActivity" src/

2Fix

Make every class that uses the trait extend a class that has the method, or change the trait so it does not call parent::.

trait LogsActivity
{
    public function save(): void
    {
        // runs the using class's parent method; the class must extend one
        parent::save();
        // log here
    }
}

class Order extends Model
{
    use LogsActivity;
}

3Verify

Call the trait method on an object of each class that uses it.

php -r 'require "vendor/autoload.php"; (new App\Order())->save();'

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Run php -l and a static analyser (PHPStan or Psalm) in CI: they flag parent:: in a class without a parent before it runs.
  • When you copy a method from a child class, check whether the new class extends the same base.
  • Prefer composition (a property holding the other object) over a trait that depends on parent:: methods.

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.