PHP

PHP Classes, Interfaces, & Traits

Below are some general characteristics of classes, interfaces, and traits. The lists are incomplete and not detailed but capture some important features to understand them.

🟩 CLASSES

Can:

  • CAN have a constructor
  • CAN have zero or more data members and/or methods
  • CAN be used to instantiate objects
  • CAN only extend a maximum of one Class (abstract or concrete)
  • CAN implement many Interfaces
  • CAN use many Traits

Cannot:

  • CANNOT have abstract methods
  • CANNOT have nested Classes

🟩 TRAITS

Can:

  • CAN have a constructor
  • CAN have data fields
  • CAN have methods
  • CAN use many Traits

Cannot:

  • CANNOT extend Classes or implement Interfaces
  • CANNOT have abstract methods
  • CANNOT be used to instantiate objects

🟩 INTERFACES

Can:

  • CAN extend many Interfaces
  • CAN have only function definitions
  • CAN only be implemented by an abstract or concrete Class

Cannot:

  • CANNOT have a constructor
  • CANNOT have data members
  • CANNOT have abstract methods
  • CANNOT be used to instantiate objects
  • CANNOT extend Classes or use Traits
Verified by MonsterInsights