PHP

PHP

Made by DeepSource

Invalid use of class method overriding PHP-E1005

Bug risk
Critical

The method you are trying to override cannot be overridden in the child class. This would result in a runtime error which can break the execution flow.

This issue can be raised for any of the following cases:

  • Overriding a final method from the parent class
  • Overriding a method with different access visibility than the parent class
  • Overriding a static method as a non-static method or vice versa
  • Overriding a method with parameter types different from the method in the parent class
  • Overridden method's parameters do not match with the parent class's parameters

Bad practice

class Member
{
    final public function getName(string $firstname, string $lastname): string
    {
        return $firstname . ' ' . $lastname;
    }
}

class User extends Member
{
    // getName is final method, hence cannot be overridden
    public function getName(string $firstname, string $lastname): string
    {
        return $firstname;
    }
}
class Member
{
    public function getName(string $firstname, string $lastname): string
    {
        return $firstname . ' ' . $lastname;
    }
}

class User extends Member
{
    // getName is public method in parent class so it can't be overridden as a private method
    private function getName(string $firstname, string $lastname): string
    {
        return $firstname;
    }
}
class Member
{
    public static function getName(string $firstname, string $lastname): string
    {
        return $firstname . ' ' . $lastname;
    }
}

class User extends Member
{
    // getName is static method in parent class so it can't be overridden as non-static method
    public function getName(string $firstname, string $lastname): string
    {
        return $firstname;
    }
}
class Member
{
    public function getName(string $firstname, string $lastname): string
    {
        return $firstname . ' ' . $lastname;
    }
}

class User extends Member
{
    // parameter #1 $id(int) is not matching parameter #1 $firstname(string) from parent class
    public function getName(int $id, string $firstname): string
    {
        return '#' . $id . ' ' . $firstname;
    }
}
class Member
{
    public function getName(string $firstname, string $lastname): string
    {
        return $firstname . ' ' . $lastname;
    }
}

class User extends Member
{
    // getName method has to override all the required parameters from parent class
    public function getName(string $firstname): string
    {
        return $firstname;
    }
}

Recommended

class Member
{
    public function getName(string $firstname, string $lastname): string
    {
        return $firstname . ' ' . $lastname;
    }
}

class User extends Member
{
    public function getName(string $firstname, string $lastname): string
    {
        return $firstname;
    }
}
class Member
{
    public static function getName(string $firstname, string $lastname): string
    {
        return $firstname . ' ' . $lastname;
    }
}

class User extends Member
{
    public static function getName(string $firstname, string $lastname): string
    {
        return $firstname;
    }
}
class Member
{
    public function getName(string $firstname, string $lastname): string
    {
        return $firstname . ' ' . $lastname;
    }
}

class User extends Member
{
    public function getId(int $id)
    {
        return $id;
    }

    public function getName(string $firstname, string $lastname): string
    {
        return '#' . $this->getId() . ' ' . $firstname;
    }
}

References