PHP PHP Object-Oriented Programming 2 — Questions and Answers
Question 1: What does the abstract keyword mean when applied to a PHP class?
- The class has no properties
- The class cannot be instantiated directly (Correct answer)
- The class has no constructor
- The class is read-only
Correct answer: The class cannot be instantiated directly
An abstract class cannot be instantiated on its own and must be subclassed before use.
Question 2: What does an interface in PHP define?
- A concrete class with default values
- A contract of methods that implementing classes must define (Correct answer)
- A static utility class
- A class that can't be extended
Correct answer: A contract of methods that implementing classes must define
An interface declares method signatures without implementation, and any class that implements it must provide concrete implementations.
Question 3: Which keyword allows calling a PHP method without creating a class instance?
- final
- abstract
- static (Correct answer)
- global
Correct answer: static
The static keyword allows properties and methods to belong to the class itself rather than instances, callable via ClassName::method().
Question 4: How do you call the parent class's constructor from a child class in PHP?
- super()
- base::__construct()
- parent::__construct() (Correct answer)
- this->parent()
Correct answer: parent::__construct()
parent::__construct() explicitly calls the parent class's constructor from within the child class.
Question 5: What does the final keyword prevent when applied to a PHP method?
- The method from being made static
- Child classes from overriding the method (Correct answer)
- The method from accepting parameters
- The method from returning a value
Correct answer: Child classes from overriding the method
A final method cannot be overridden in any subclass, locking its behavior.
Question 6: Which visibility modifier allows access from the class itself and all subclasses, but not from outside?
- public
- private
- protected (Correct answer)
- internal
Correct answer: protected
protected members are accessible within the declaring class and any class that inherits from it.
What does the abstract keyword mean when applied to a PHP class?