0
0
PHPprogramming~10 mins

Parent keyword behavior in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Parent keyword behavior
Define Parent Class with method
Define Child Class extends Parent
Override method in Child
Call parent::method() inside Child method
Execute Child method
Parent method runs, then Child continues
Shows how a child class calls a method from its parent class using the parent keyword inside an overridden method.
Execution Sample
PHP
<?php
class ParentClass {
  function greet() {
    echo "Hello from Parent\n";
  }
}

class ChildClass extends ParentClass {
  function greet() {
    parent::greet();
    echo "Hello from Child\n";
  }
}

$obj = new ChildClass();
$obj->greet();
?>
This code shows a child class calling its parent's greet method before adding its own message.
Execution Table
StepActionEvaluationOutput
1Create ChildClass objectObject created
2Call greet() on ChildClass objectChildClass::greet() runs
3Inside ChildClass::greet(), call parent::greet()ParentClass::greet() runsHello from Parent
4Continue ChildClass::greet() after parent callEcho child messageHello from Child
5Method greet() endsReturn to caller
💡 Method greet() completes after printing both parent and child messages
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
$objnullChildClass objectChildClass objectChildClass objectChildClass object
Key Moments - 2 Insights
Why do we use parent::greet() inside the child method?
Using parent::greet() calls the original method from the parent class, allowing the child to add behavior without replacing the parent's code entirely, as shown in step 3 of the execution_table.
What happens if we omit parent::greet() in the child method?
If omitted, only the child's message prints because the parent's greet method is not called, so the output would miss "Hello from Parent", as seen by comparing step 3 and 4.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is printed at step 3?
A"Hello from Child"
B"Hello from Parent"
CNothing is printed
DAn error occurs
💡 Hint
Check the Output column at step 3 in the execution_table
At which step does the child class add its own message?
AStep 4
BStep 3
CStep 2
DStep 5
💡 Hint
Look at the Action and Output columns in the execution_table for when "Hello from Child" is printed
If we remove parent::greet() call, what changes in variable_tracker?
A$obj becomes null
B$obj changes to ParentClass object
CNo change in $obj variable values
D$obj is undefined
💡 Hint
Variable $obj remains the same object regardless of method calls, see variable_tracker
Concept Snapshot
Parent keyword in PHP lets a child class call a method from its parent class.
Use parent::methodName() inside an overridden method.
This allows extending behavior without replacing it.
Without parent::, only child method runs.
Useful for code reuse and adding extra steps.
Full Transcript
This example shows how a child class in PHP can call a method from its parent class using the parent keyword. First, a ParentClass defines a greet method that prints a message. Then, ChildClass extends ParentClass and overrides greet. Inside ChildClass's greet, it calls parent::greet() to run the parent's method, then prints its own message. When we create a ChildClass object and call greet, the output shows the parent's message followed by the child's message. The execution table traces each step: creating the object, calling the method, calling the parent method, printing the child's message, and ending the method. The variable tracker shows the object stays the same throughout. Key moments clarify why parent:: is used and what happens if omitted. The quiz tests understanding of output at each step and variable state. This helps beginners see how inheritance and method overriding work with parent keyword in PHP.