0
0
PHPprogramming~5 mins

Constructor inheritance in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What is constructor inheritance in PHP?
Constructor inheritance means a child class can use the constructor method of its parent class to set up initial values or states.
Click to reveal answer
beginner
How do you call a parent class constructor inside a child class in PHP?
You use <code>parent::__construct()</code> inside the child class constructor to run the parent constructor.
Click to reveal answer
intermediate
What happens if a child class defines its own constructor but does not call <code>parent::__construct()</code>?
The parent constructor will NOT run automatically. The child constructor overrides it, so you must call the parent constructor explicitly if needed.
Click to reveal answer
beginner
True or False: If a child class does not define a constructor, it automatically inherits the parent's constructor in PHP.
True. The child class will use the parent's constructor if it does not have its own.
Click to reveal answer
beginner
Why might you want to call the parent constructor inside a child constructor?
To make sure the parent class sets up its part of the object correctly before the child adds or changes anything.
Click to reveal answer
In PHP, how do you call the parent class constructor from a child class?
Aparent::__construct()
Bsuper()
Cthis->__construct()
Dbase::__construct()
If a child class has no constructor, what happens when you create an object of that child class?
AThe child class constructor runs
BNo constructor runs
CAn error occurs
DThe parent's constructor runs automatically
What happens if a child class defines a constructor but does NOT call parent::__construct()?
AOnly the parent constructor runs
BBoth parent and child constructors run automatically
COnly the child constructor runs
DPHP throws an error
Why is constructor inheritance useful?
AIt lets child classes reuse setup code from parent classes
BIt prevents child classes from having constructors
CIt makes PHP run faster
DIt disables object creation
Which keyword is used to refer to the parent class in PHP?
Athis
Bparent
Cbase
Dsuper
Explain how constructor inheritance works in PHP and how to properly use it in a child class.
Think about when the parent's constructor runs and how to call it explicitly.
You got /3 concepts.
    Describe a real-life example where calling a parent constructor from a child class is important.
    Imagine building a car where the base car sets wheels and engine, and the child adds special features.
    You got /3 concepts.