Bird
Raised Fist0
PHPprogramming~10 mins

Method overriding in PHP - Step-by-Step Execution

Choose your learning style10 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.

Practice

(1/5)
1. What does method overriding allow a child class to do in PHP?
easy
A. Change the behavior of a method inherited from the parent class
B. Create a new method with a different name
C. Prevent the parent class from being instantiated
D. Automatically call all parent methods

Solution

  1. Step 1: Understand method overriding concept

    Method overriding means a child class uses the same method name as the parent to change its behavior.
  2. Step 2: Identify what overriding does

    It replaces or modifies the parent's method behavior in the child class.
  3. Final Answer:

    Change the behavior of a method inherited from the parent class -> Option A
  4. Quick Check:

    Method overriding = change parent method behavior [OK]
Hint: Overriding means same method name, new behavior [OK]
Common Mistakes:
  • Thinking overriding creates new methods with different names
  • Confusing overriding with inheritance itself
  • Believing overriding prevents parent instantiation
2. Which of the following is the correct syntax to override a method named greet in a child class in PHP?
easy
A. function greetChild() { /* new code */ }
B. function greet() { /* new code */ }
C. override function greet() { /* new code */ }
D. parent::greet() { /* new code */ }

Solution

  1. Step 1: Recall method overriding syntax

    To override, define a method with the same name in the child class.
  2. Step 2: Check options for correct syntax

    Only function greet() { /* new code */ } defines function greet() correctly without extra keywords.
  3. Final Answer:

    function greet() { /* new code */ } -> Option B
  4. Quick Check:

    Same method name in child = override [OK]
Hint: Use same method name in child class to override [OK]
Common Mistakes:
  • Adding 'override' keyword which PHP does not use
  • Changing method name to something else
  • Trying to call parent method syntax incorrectly
3. What will be the output of this PHP code?
class ParentClass {
  function sayHello() {
    echo "Hello from Parent";
  }
}
class ChildClass extends ParentClass {
  function sayHello() {
    echo "Hello from Child";
  }
}
$obj = new ChildClass();
$obj->sayHello();
medium
A. Hello from ParentChild
B. Hello from Parent
C. Hello from Child
D. Error: Method not found

Solution

  1. Step 1: Identify method overriding in ChildClass

    ChildClass defines sayHello() which overrides ParentClass method.
  2. Step 2: Check which method is called

    Creating ChildClass object calls ChildClass's sayHello(), printing "Hello from Child".
  3. Final Answer:

    Hello from Child -> Option C
  4. Quick Check:

    Child method overrides parent = "Hello from Child" [OK]
Hint: Child method runs if overridden, not parent [OK]
Common Mistakes:
  • Expecting parent method output instead of child
  • Thinking both outputs print together
  • Assuming error due to method name clash
4. Find the error in this PHP code that tries to override a method:
class Base {
  function show() {
    echo "Base";
  }
}
class Derived extends Base {
  function show() {
    parent.show();
    echo " Derived";
  }
}
$obj = new Derived();
$obj->show();
medium
A. Class Derived must declare show() as public
B. Cannot override methods in PHP
C. Missing return statement in show()
D. Incorrect syntax to call parent method; should use parent::show()

Solution

  1. Step 1: Identify how parent method is called

    The code uses parent.show() which is invalid syntax in PHP.
  2. Step 2: Correct syntax for calling parent method

    PHP requires parent::show() to call the parent's method inside child.
  3. Final Answer:

    Incorrect syntax to call parent method; should use parent::show() -> Option D
  4. Quick Check:

    Call parent method with parent::method() [OK]
Hint: Use parent::method() to call parent method inside child [OK]
Common Mistakes:
  • Using dot (.) instead of :: to call parent method
  • Thinking overriding is not allowed
  • Forgetting to call parent method when needed
5. Given these classes, what will be the output?
class Animal {
  function sound() {
    echo "Some sound";
  }
}
class Dog extends Animal {
  function sound() {
    parent::sound();
    echo " and Bark";
  }
}
class Puppy extends Dog {
  function sound() {
    echo "Yip";
  }
}
$pet = new Puppy();
$pet->sound();
hard
A. Yip
B. Some sound and Bark and Yip
C. Some sound and Bark
D. Error: Cannot override multiple times

Solution

  1. Step 1: Trace method calls for Puppy object

    Puppy overrides sound() and only echoes "Yip" without calling parent.
  2. Step 2: Understand overriding chain

    Since Puppy method does not call parent::sound(), Dog and Animal methods are not executed.
  3. Final Answer:

    Yip -> Option A
  4. Quick Check:

    Child override without parent call = only child output [OK]
Hint: If child method skips parent call, only child output shows [OK]
Common Mistakes:
  • Assuming all parent methods run automatically
  • Expecting combined outputs without explicit parent calls
  • Thinking multiple overrides cause errors
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard