0
0
PHPprogramming~5 mins

Parent keyword behavior in PHP - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the parent keyword do in PHP?
The <code>parent</code> keyword is used to access methods or properties from a parent class inside a child class.
Click to reveal answer
beginner
How do you call a parent class constructor using <code>parent</code>?
Inside the child class constructor, you call <code>parent::__construct()</code> to run the parent class constructor.
Click to reveal answer
intermediate
Can parent be used to access overridden methods?
Yes, <code>parent</code> lets you call the parent class version of a method even if the child class has overridden it.
Click to reveal answer
intermediate
What happens if you use <code>parent</code> in a class without a parent?
Using <code>parent</code> in a class without a parent class causes a fatal error because there is no parent to refer to.
Click to reveal answer
beginner
Example: How to call a parent method named show() inside a child class?
Use <code>parent::show();</code> inside the child class method to call the parent's <code>show()</code> method.
Click to reveal answer
What does parent::methodName() do in PHP?
ADeletes the method <code>methodName</code>
BCalls the method <code>methodName</code> from the child class
CCreates a new method named <code>methodName</code>
DCalls the method <code>methodName</code> from the parent class
How do you call the parent constructor inside a child class constructor?
Aparent::__construct()
Bparent::constructor()
Cthis->parent()
Dsuper::__construct()
What error occurs if parent is used in a class without a parent?
AWarning: Undefined variable
BFatal error: Cannot access parent
CParse error: Unexpected token
DNo error, it works fine
Can parent access private properties of the parent class?
AYes, always
BOnly if declared static
CNo, private properties are not accessible
DOnly if accessed via a method
Which keyword is used to refer to the parent class in PHP?
Aparent
Bsuper
Cbase
Dthis
Explain how the parent keyword works in PHP and give an example of calling a parent method.
Think about how a child class can reuse code from its parent.
You got /3 concepts.
    Describe what happens if you try to use parent in a class that does not extend any other class.
    Consider what <code>parent</code> means and what happens if it has nothing to refer to.
    You got /3 concepts.