Maaz Zindani

Loading the index…

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

PHP fatal error: your class has abstract methods it does not implement (Symfony)

ErrorClass must be declared abstract or implement the remaining methods

Short answer

PHP raises this fatal error when a class implements an interface or extends an abstract class but one required method is missing, or is declared with a signature that does not match. The error names the missing method in brackets. Add that method with a compatible signature, or mark the class abstract if it is only a base class.

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

Fix it fast

  1. 1Find out which cause you have

    Read the part of the message in brackets: it is Interface::method or ParentClass::method. That is the method PHP cannot find. Then list what your class already declares.

    php -l src/Entity/User.php
    grep -n "function \|implements\|extends" src/Entity/User.php
  2. 2Match what you see, then jump to its fix

    1. 1the bracket names UserInterface::getUserIdentifier, getRoles or PasswordAuthenticatedUserInterface::getPasswordA Symfony User class lacks a method that UserInterface requiresFix
    2. 2the bracket names AbstractSomething::method, a class that is yours or from a vendor packageA child class does not define every abstract method of its parentFix
    3. 3the same fatal error right after a package upgrade, although a method with that name existsThe method exists but its signature is not compatibleFix

What does the error look like?

  • Class must be declared abstract or implement the remaining methods

Cause 1 of 3

A Symfony User class lacks a method that UserInterface requires

1Check

Symfony asks a user class to implement UserInterface, and PasswordAuthenticatedUserInterface when passwords are stored. The generated class provides getUserIdentifier(), getRoles() and getPassword(). A class written for an older release, or edited by hand, often lacks one of them.

grep -n "getUserIdentifier\|getRoles\|getPassword" src/Entity/User.php

2Fix

Add the missing method. For a class that uses an email as the login, getUserIdentifier() returns that email as a string. Regenerating a fresh class with the maker command and comparing it with yours shows every method that is expected.

public function getUserIdentifier(): string
{
    return (string) $this->email;
}

# to see the reference version:
php bin/console make:user

3Verify

Lint the file and clear the cache; the fatal error is gone.

php -l src/Entity/User.php
php bin/console cache:clear

Confirmed by symfony.com

Did this fix it?

Cause 2 of 3

A child class does not define every abstract method of its parent

1Check

Open the parent class and find the methods marked abstract. PHP requires the child to define all of them. A class with even one abstract method must itself be abstract.

grep -n "abstract " vendor/vendor-name/package-name/src/ParentClass.php

2Fix

Implement each abstract method in the child class. If the class is only meant to be extended, declare it abstract instead of implementing anything.

abstract class BaseReport
{
    abstract protected function build(): array;
}

final class SalesReport extends BaseReport
{
    protected function build(): array
    {
        return [];
    }
}

3Verify

Load the class once so PHP compiles it.

php -l src/Report/SalesReport.php && php -r 'require "vendor/autoload.php"; new App\Report\SalesReport();'

Confirmed by php.net

Did this fix it?

Cause 3 of 3

The method exists but its signature is not compatible

1Check

The manual says implemented abstract methods must follow the usual inheritance and signature compatibility rules. A different parameter list, or a changed return type breaks the match.

grep -n "function build" vendor/vendor-name/package-name/src/ParentClass.php
grep -n "function build" src/Report/SalesReport.php

2Fix

Copy the parameter list, visibility and return type from the parent declaration. A child may add optional parameters, but may not remove or retype the required ones.

// parent: abstract protected function prefixName($name);
public function prefixName($name, $separator = ".")
{
    return $name . $separator;
}

3Verify

Lint the file again.

php -l src/Report/SalesReport.php

Confirmed by php.net

Did this fix it?

How do I stop it coming back?

  • Generate user classes with the maker command instead of copying one from an older project.
  • After a Composer upgrade, read the changelog of any package whose interface or abstract class you implement.
  • Add return types to methods you write, so a mismatch shows up in your editor before it reaches the server.
  • Run php -l and your test suite in CI on every pull request.

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.