Bird
0
0

Identify the error in this PHP code:

medium📝 Debug Q14 of 15
PHP - Inheritance and Polymorphism
Identify the error in this PHP code:
class ParentClass {
  public function sayHi() {
    echo "Hi from parent";
  }
}

class ChildClass extends ParentClass {
  public function sayHi() {
    parent.sayHi();
    echo " and child";
  }
}

$obj = new ChildClass();
$obj->sayHi();
AUsing dot (.) instead of scope resolution (::) to call parent method
BMissing parentheses after method name
CChild class cannot override parent methods
DParent class method is private
Step-by-Step Solution
Solution:
  1. Step 1: Check how parent method is called

    The code uses parent.sayHi(); which is invalid syntax in PHP.
  2. Step 2: Correct syntax uses scope resolution operator

    It should be parent::sayHi(); to call the parent method inside child.
  3. Final Answer:

    Using dot (.) instead of scope resolution (::) to call parent method -> Option A
  4. Quick Check:

    Use parent::method(), not parent.method() [OK]
Quick Trick: Use ::, not . to call parent methods [OK]
Common Mistakes:
  • Using dot (.) operator for method calls
  • Forgetting parentheses after method name
  • Assuming private methods can be called by child

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More PHP Quizzes