0
0
PHPprogramming~10 mins

Method overriding in PHP - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Method overriding
Define Parent Class with Method
Define Child Class with Same Method
Create Child Object
Call Method on Child Object
Child's Method Runs, Overrides Parent's
Output Result
The child class defines a method with the same name as the parent class, so when called on the child object, the child's method runs instead of the parent's.
Execution Sample
PHP
<?php
class ParentClass {
  function greet() {
    echo "Hello from Parent\n";
  }
}
class ChildClass extends ParentClass {
  function greet() {
    echo "Hello from Child\n";
  }
}
$obj = new ChildClass();
$obj->greet();
?>
This code shows a parent and child class both with a greet method; calling greet on the child object runs the child's version.
Execution Table
StepActionClassMethod CalledOutput
1Define ParentClass with greet()ParentClassN/AN/A
2Define ChildClass extending ParentClass with greet()ChildClassN/AN/A
3Create object of ChildClassChildClassN/AN/A
4Call greet() on ChildClass objectChildClassgreet()Hello from Child
5End of scriptN/AN/AN/A
💡 Script ends after calling greet() on ChildClass object which overrides ParentClass method
Variable Tracker
VariableStartAfter Step 3Final
$objundefinedChildClass objectChildClass object
Key Moments - 2 Insights
Why does calling greet() on the child object run the child's method, not the parent's?
Because in step 4 of the execution_table, the method called is greet() from ChildClass, which overrides the parent's greet() method.
What happens if the child class does not define greet()?
Then calling greet() on the child object would run the parent's greet() method, since the child inherits it without overriding.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output when greet() is called on the child object?
ANo output
BHello from Parent
CHello from Child
DError
💡 Hint
Check step 4 in the execution_table where greet() is called on ChildClass object.
At which step is the child class's greet() method defined?
AStep 1
BStep 2
CStep 3
DStep 4
💡 Hint
Look at the action column in the execution_table for method definitions.
If the child class did not override greet(), what would be the output at step 4?
AHello from Parent
BHello from Child
CNo output
DError
💡 Hint
Think about inheritance and method overriding explained in key_moments.
Concept Snapshot
Method overriding in PHP:
- Child class defines a method with the same name as parent.
- Child's method replaces parent's when called on child object.
- Use extends keyword for inheritance.
- Calling method on child object runs child's version.
- If child doesn't override, parent's method runs.
Full Transcript
This example shows method overriding in PHP. First, a parent class defines a greet method. Then a child class extends the parent and defines its own greet method with the same name. When we create an object of the child class and call greet, the child's greet method runs, printing 'Hello from Child'. This happens because the child's method overrides the parent's. If the child did not define greet, calling greet on the child object would run the parent's method instead. This is how method overriding lets child classes change or extend behavior of parent classes.