0
0
PHPprogramming~5 mins

Interface declaration and implementation in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an interface in PHP?
An interface in PHP is a contract that defines methods a class must implement, without providing the method bodies.
Click to reveal answer
beginner
How do you declare an interface in PHP?
Use the interface keyword followed by the interface name and method signatures inside curly braces.
Click to reveal answer
beginner
What keyword is used by a class to implement an interface?
The <code>implements</code> keyword is used by a class to promise it will provide all methods defined in the interface.
Click to reveal answer
intermediate
Can a class implement multiple interfaces in PHP?
Yes, a class can implement multiple interfaces by listing them separated by commas after the <code>implements</code> keyword.
Click to reveal answer
intermediate
What happens if a class does not implement all methods of an interface?
PHP will throw a fatal error because the class breaks the contract defined by the interface.
Click to reveal answer
Which keyword is used to declare an interface in PHP?
Aclass
Bextends
Cimplements
Dinterface
How does a class promise to follow an interface's contract?
Ausing the <code>extends</code> keyword
Busing the <code>implements</code> keyword
Cby defining the interface inside the class
Dby calling interface methods directly
Can an interface contain method bodies in PHP?
AOnly if methods are static
BYes, always
CNo, never
DOnly if methods are abstract
What happens if a class misses implementing a method from an interface?
APHP throws a fatal error
BPHP ignores the missing method
CThe class works but with warnings
DThe interface is automatically updated
How can a class implement multiple interfaces?
AList interfaces separated by commas after <code>implements</code>
BUse multiple <code>implements</code> keywords
CUse <code>extends</code> keyword
DDefine interfaces inside the class
Explain what an interface is in PHP and how a class uses it.
Think of an interface as a promise a class makes.
You got /4 concepts.
    Describe what happens if a class does not implement all methods from an interface it declares to implement.
    PHP is strict about interfaces.
    You got /4 concepts.