PHP

PHP

Made by DeepSource

Invalid use of implements keyword PHP-W1008

Bug risk
Critical

A class can only be able to implement an interface using implements keyword. Trying to use implements keyword to reference anything other than an interface, can result in fatal error.

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

  • The interface you are trying to implement doesn't exist.
  • The interface is referenced with incorrect case.
  • Trying to implement a class or trait instead of an interface.

Bad practice

// invalid: interface "Email" doesn't exist
class Sendgrid implements Email
{
    public function send(): void
    {
    }
}
inteface Email
{
    public function send(): void;
}

// invalid: interface "email" is referenced with incorrect case
class Sendgrid implements email
{
    public function send(): void
    {
    }
}
class Email
{
    public function send(): void
    {
    }
}

// invalid: "Email" is not an interface, it's a class
class Sendgrid implements Email
{
    public function send(): void
    {
    }
}
trait EmailTrait
{
    public function send(): void
    {
    }
}

// invalid: "EmailTrait" is not an interface, it's a trait
class Sendgrid implements EmailTrait
{
    public function send(): void
    {
    }
}

Recommended

interface Email
{
    public function send(): void;
}

class Sendgrid implements Email
{
    public function send(): void
    {
    }
}

Use extends keyword when trying to include a class inside another class:

class Email
{
    public function send(): void
    {
    }
}

class Sendgrid extends Email
{
    public function send(): void
    {
        parent::send();
    }
}

Use use keyword when trying to include a trait inside a class:

trait EmailTrait
{
    public function send(): void
    {
    }
}

class Sendgrid
{
    use EmailTrait;
}

References