PHP logoPHP/
PHP-W1010

Invalid use of `extends` keywordPHP-W1010

Critical severityCritical
Bug Risk categoryBug Risk

A class can only inherit from another class using the extends keyword. Trying to use the extends keyword to reference anything other than a class will result in a fatal error.

This issue will be raised in any of the following cases:

  • The class you are trying to extend doesn't exist or is not in scope.
  • There is a typo in the name of the extended class.
  • The class you are trying to extend is a final class.
  • You are trying to extend a trait or an interface instead of a class.

Bad practice

// invalid: class "AppController" doesn't exist or is not in scope
class UserController extends AppController
{
    public function index(): void
    {
    }
}
class AppController {}

// invalid: It's "AppController", not "Appcontroller"
class UserController extends Appcontroller
{
    public function index(): void
    {
    }
}
final class AppController {}

// invalid: class "AppController" is a final class
class UserController extends Appcontroller
{
    public function index(): void
    {
    }
}
interface Create
{
    public function create(): void;
}

// invalid: "Create" is not a class, it's an interface
class UserController extends Create
{
    public function index(): void
    {
    }
}
trait BaseController
{
}

// invalid: "BaseController" is not a class, it's a trait
class UserController extends BaseController
{
    public function index(): void
    {
    }
}
class AppController {}

class UserController extends AppController
{
    public function index(): void
    {
    }
}

Use implements to implement an interface inside a class:

interface Create
{
    public function create(): void;
}

class UserController implements Create
{
    public function index(): void
    {
    }
}

Use use to include a trait inside a class:

trait BaseController
{
}

class UserController
{
    use BaseController;

    public function index(): void
    {
    }
}

References