0
0
PHPprogramming~5 mins

Abstract classes and methods in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is an abstract class in PHP?
An abstract class in PHP is a class that cannot be instantiated on its own. It is meant to be a base class that other classes extend. It can contain abstract methods that must be implemented by child classes.
Click to reveal answer
beginner
What is an abstract method?
An abstract method is a method declared in an abstract class without a body. Child classes must provide their own implementation of this method.
Click to reveal answer
beginner
Can you create an object from an abstract class directly?
No, you cannot create an object directly from an abstract class. You must extend it with a child class and instantiate the child class instead.
Click to reveal answer
intermediate
Why use abstract classes and methods?
Abstract classes and methods help define a common template for related classes. They enforce that certain methods are implemented in child classes, ensuring consistent behavior.
Click to reveal answer
beginner
Example of declaring an abstract class with an abstract method in PHP?
Example:<br><pre>abstract class Animal {
  abstract public function makeSound();
}

class Dog extends Animal {
  public function makeSound() {
    echo "Woof!";
  }
}</pre>
Click to reveal answer
Which keyword is used to declare an abstract class in PHP?
Astatic
Binterface
Cfinal
Dabstract
What happens if a child class does not implement an abstract method from its parent abstract class?
APHP throws a fatal error
BThe code runs without errors
CThe child class will be abstract too
DThe method is ignored
Can an abstract class contain non-abstract methods?
AYes, it can contain both abstract and non-abstract methods
BNo, all methods must be abstract
COnly static methods are allowed
DOnly private methods are allowed
Which of these is true about abstract classes?
AThey can be instantiated directly
BThey cannot have properties
CThey can implement interfaces
DThey must have at least one abstract method
How do you declare an abstract method in PHP?
Afunction methodName() {}
Babstract public function methodName();
Cpublic function methodName() {}
Dstatic function methodName();
Explain what an abstract class is and why you would use one in PHP.
Think about a blueprint that sets rules for other classes.
You got /4 concepts.
    Describe how abstract methods work and what happens if a child class does not implement them.
    Consider a promise that child classes must keep.
    You got /3 concepts.