PHP: Interface vs Abstract Class vs Trait

Here is a comparison between Interface, Abstract class, and Trait in PHP. Let’s take a look at that side-by-side comparison in a table-

Criteria / RuleInterfaceAbstract ClassTrait
Can be instantiatedNo NoNo NoNo No
Can contain public propertyNo NoYes YesYes Yes
Can contain protected propertyNo NoYes YesYes Yes
Can contain private propertyNo NoYes YesYes Yes
Can contain public constantYes YesYes YesYes Yes
Can contain protected constantNo NoYes YesYes Yes
Can contain private constantNo NoYes YesYes Yes
Can contain public abstract method(signature only)Yes YesYes YesYes Yes
Can contain protected abstract method(signature only)No NoYes YesYes Yes
Can contain private abstract method(signature only)No NoNo NoYes Yes
Can contain public method(normal)No NoYes YesYes Yes
Can contain protected method(normal)No NoYes YesYes Yes
Can contain private method(normal)No NoYes YesYes Yes
Can inherit from normal classNo NoYes YesNo No
Can inherit from an abstract classNo NoYes YesNo No
Can implement interface(s)Yes YesYes YesNo No
Can use trait(s)No NoYes YesYes Yes
Can be inherited/used multiple times
from a single class/interface/trait
Yes YesNo NoYes Yes

Now let’s discuss in detail, about the use cases and when should we choose one of these, in our PHP code.

Leave a Comment


The reCAPTCHA verification period has expired. Please reload the page.