Bird
Raised Fist0
PHPprogramming~20 mins

Parent keyword behavior in PHP - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Master of Parent Keyword in PHP
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of parent:: method call in inheritance
What is the output of the following PHP code?
PHP
<?php
class A {
    public function greet() {
        return "Hello from A";
    }
}
class B extends A {
    public function greet() {
        return parent::greet() . " and B";
    }
}
$b = new B();
echo $b->greet();
?>
AHello from A and B
BHello from B
CHello from A
DFatal error: Cannot access parent::greet()
Attempts:
2 left
💡 Hint
Remember that parent:: calls the method from the parent class.
❓ Predict Output
intermediate
2:00remaining
Using parent:: in constructor
What will be the output of this PHP code?
PHP
<?php
class ParentClass {
    public function __construct() {
        echo "Parent constructor called. ";
    }
}
class ChildClass extends ParentClass {
    public function __construct() {
        parent::__construct();
        echo "Child constructor called.";
    }
}
new ChildClass();
?>
AChild constructor called.
BParent constructor called. Child constructor called.
CParent constructor called.
DFatal error: Cannot call parent constructor
Attempts:
2 left
💡 Hint
Check how constructors are called in inheritance.
❓ Predict Output
advanced
2:00remaining
Output when parent method is private
What happens when a child class tries to call a private parent method using parent:: ?
PHP
<?php
class Base {
    private function secret() {
        return "Secret from Base";
    }
    public function reveal() {
        return $this->secret();
    }
}
class Derived extends Base {
    public function secret() {
        return "Secret from Derived";
    }
    public function revealSecret() {
        return parent::secret();
    }
}
$d = new Derived();
echo $d->reveal();
echo " - ";
echo $d->secret();
echo " - ";
echo $d->revealSecret();
?>
AFatal error - Secret from Derived - Secret from Base
BSecret from Base - Secret from Derived - Secret from Base
CSecret from Base - Secret from Derived - Fatal error
DSecret from Base - Fatal error - Secret from Derived
Attempts:
2 left
💡 Hint
Private methods are not accessible via parent:: in child classes.
❓ Predict Output
advanced
2:00remaining
parent:: with static methods and late static binding
What is the output of this PHP code involving static methods and parent::?
PHP
<?php
class Alpha {
    public static function who() {
        return "Alpha";
    }
    public static function test() {
        return static::who();
    }
}
class Beta extends Alpha {
    public static function who() {
        return "Beta";
    }
    public static function test() {
        return parent::test();
    }
}
echo Beta::test();
?>
ABetaBeta
BAlpha
CFatal error: Cannot access parent::test()
DBeta
Attempts:
2 left
💡 Hint
Remember that static:: uses late static binding to call the method in the called class.
❓ Predict Output
expert
3:00remaining
Complex parent:: calls with traits and multiple inheritance
What is the output of this PHP code involving traits and parent:: calls?
PHP
<?php
trait T {
    public function say() {
        return "Trait says hi";
    }
}
class Base {
    public function say() {
        return "Base says hi";
    }
}
class Child extends Base {
    use T;
    public function say() {
        return parent::say() . " and " . T::say();
    }
}
$c = new Child();
echo $c->say();
?>
AFatal error: Cannot access trait method T::say() directly
BBase says hi and Trait says hi
CTrait says hi and Base says hi
DBase says hi
Attempts:
2 left
💡 Hint
Traits methods cannot be called statically like T::say() inside an instance method.

Practice

(1/5)
1. What does the parent keyword do in PHP classes?
easy
A. It renames the parent class.
B. It creates a new instance of the parent class.
C. It deletes the parent class methods from the child class.
D. It allows a child class to access methods or properties from its parent class.

Solution

  1. Step 1: Understand the role of parent keyword

    The parent keyword in PHP is used inside a child class to refer to its parent class.
  2. Step 2: Identify what parent allows

    It allows the child class to call or extend methods or properties defined in the parent class.
  3. Final Answer:

    It allows a child class to access methods or properties from its parent class. -> Option D
  4. Quick Check:

    parent accesses parent methods/properties [OK]
Hint: Remember: parent calls parent class features [OK]
Common Mistakes:
  • Thinking parent creates new objects
  • Confusing parent with $this
  • Believing parent deletes methods
2. Which of the following is the correct syntax to call a parent class method named show inside a child class method?
easy
A. parent.show();
B. parent->show();
C. parent::show();
D. parent:show();

Solution

  1. Step 1: Recall PHP syntax for calling parent methods

    In PHP, to call a parent class method inside a child, use the scope resolution operator :: with parent.
  2. Step 2: Match the correct syntax

    The correct syntax is parent::methodName();, so for method show, it is parent::show();.
  3. Final Answer:

    parent::show(); -> Option C
  4. Quick Check:

    Use :: with parent to call methods [OK]
Hint: Use double colon (::) to call parent methods [OK]
Common Mistakes:
  • Using arrow (->) instead of double colon (::)
  • Using dot (.) operator which is for strings
  • Using single colon (:) which is invalid syntax
3. What will be the output of this PHP code?
class A {
  public function greet() {
    return "Hello from A";
  }
}

class B extends A {
  public function greet() {
    return parent::greet() . " and B";
  }
}

$b = new B();
echo $b->greet();
medium
A. Hello from A
B. Hello from A and B
C. Hello from B
D. Error: Cannot call parent method

Solution

  1. Step 1: Understand method overriding and parent call

    Class B overrides greet() but calls parent::greet() which returns "Hello from A".
  2. Step 2: Concatenate parent result with " and B"

    The child method returns the parent's string plus " and B", so the full string is "Hello from A and B".
  3. Final Answer:

    Hello from A and B -> Option B
  4. Quick Check:

    parent::greet() + " and B" = "Hello from A and B" [OK]
Hint: parent::method() returns parent's output, can concatenate [OK]
Common Mistakes:
  • Ignoring parent:: call and expecting only child's string
  • Thinking it causes an error
  • Confusing output with only parent's or only child's string
4. 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();
medium
A. Using dot (.) instead of scope resolution (::) to call parent method
B. Missing parentheses after method name
C. Child class cannot override parent methods
D. Parent class method is private

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]
Hint: 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
5. Given these classes, what will be the output?
class Base {
  protected function message() {
    return "Base message";
  }
}

class Derived extends Base {
  public function message() {
    return parent::message() . " extended";
  }
}

class MoreDerived extends Derived {
  public function message() {
    return parent::message() . " and more";
  }
}

$obj = new MoreDerived();
echo $obj->message();
hard
A. Base message extended and more
B. Base message and more
C. Error: Cannot access protected method
D. Base message extended

Solution

  1. Step 1: Trace method calls through inheritance

    MoreDerived::message() calls parent::message() which is Derived::message().
  2. Step 2: Derived::message() calls Base::message()

    Derived calls parent::message() which is Base::message() returning "Base message" and appends " extended".
  3. Step 3: Combine all returned strings

    So Derived returns "Base message extended" and MoreDerived appends " and more" resulting in "Base message extended and more".
  4. Final Answer:

    Base message extended and more -> Option A
  5. Quick Check:

    Chained parent calls build full string [OK]
Hint: Each parent:: call adds to string stepwise [OK]
Common Mistakes:
  • Expecting error due to protected method (allowed in child)
  • Ignoring chained parent calls
  • Assuming only base message prints
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