0
0
PHPprogramming~5 mins

Interface vs abstract class vs trait in PHP - Quick Revision & Key Differences

Choose your learning style9 modes available
Recall & Review
beginner
What is an interface in PHP?
An interface defines a contract with method signatures but no method bodies. Classes that implement the interface must provide the method implementations.
Click to reveal answer
beginner
What is an abstract class in PHP?
An abstract class can have both method signatures (abstract methods) and method bodies. It cannot be instantiated directly and is meant to be extended by other classes.
Click to reveal answer
beginner
What is a trait in PHP?
A trait is a reusable set of methods that can be included in multiple classes to share functionality without using inheritance.
Click to reveal answer
beginner
Can a PHP class implement multiple interfaces?
Yes, a PHP class can implement multiple interfaces, allowing it to follow multiple contracts.
Click to reveal answer
beginner
Can a PHP class use multiple traits?
Yes, a PHP class can use multiple traits to include different sets of reusable methods.
Click to reveal answer
Which of the following can contain method implementations in PHP?
ABoth B and C
BAbstract class
CTrait
DInterface
Can a PHP interface have properties?
AOnly if declared public
BYes, with default values
CYes, but only static properties
DNo, interfaces cannot have properties
Which keyword is used to include a trait in a PHP class?
Aimplements
Bextends
Cuse
Dinclude
Can a PHP class extend multiple abstract classes?
ANo, PHP supports only single inheritance
BYes, but only if abstract classes implement interfaces
CYes, PHP supports multiple inheritance
DOnly if using traits
What is the main purpose of traits in PHP?
ATo define method signatures
BTo share reusable code between classes
CTo enforce class contracts
DTo replace interfaces
Explain the differences between interfaces, abstract classes, and traits in PHP.
Think about what each one can contain and how they are used in classes.
You got /6 concepts.
    When would you choose to use a trait instead of an abstract class or interface in PHP?
    Consider code reuse and inheritance limitations.
    You got /4 concepts.