0
0
PHPprogramming~20 mins

Extending classes in PHP - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
PHP Class Extension Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of extended class method call

What is the output of this PHP code?

PHP
<?php
class Animal {
    public function sound() {
        return "Some sound";
    }
}

class Dog extends Animal {
    public function sound() {
        return "Bark";
    }
}

$dog = new Dog();
echo $dog->sound();
?>
ASome sound
BFatal error: Cannot redeclare method
CBark
Dnull
Attempts:
2 left
💡 Hint

Remember, when a child class defines a method with the same name as the parent, it overrides it.

Predict Output
intermediate
2:00remaining
Output when calling parent method from child

What will this PHP code output?

PHP
<?php
class Vehicle {
    public function start() {
        return "Vehicle started";
    }
}

class Car extends Vehicle {
    public function start() {
        return parent::start() . " and car is ready";
    }
}

$car = new Car();
echo $car->start();
?>
ACar started
BVehicle started and car is ready
CVehicle started
DFatal error: Call to undefined method
Attempts:
2 left
💡 Hint

Look at how parent::start() is used inside the child method.

Predict Output
advanced
2:00remaining
Output when child defines method same as parent's private

What is the output of this PHP code?

PHP
<?php
class Fruit {
    private function taste() {
        return "Sweet";
    }
}

class Apple extends Fruit {
    public function taste() {
        return "Sour";
    }
}

$apple = new Apple();
echo $apple->taste();
?>
ASour
BSweet
CFatal error: Call to private method Fruit::taste() from context
DFatal error: Cannot override private method
Attempts:
2 left
💡 Hint

Private methods are not inherited or overridable; child can define its own method with the same name.

📝 Syntax
advanced
2:00remaining
Which code causes a syntax error in class extension?

Which of the following PHP code snippets will cause a syntax error?

A<?php class A { public function foo() {} } class B extends A { public function foo() { parent.foo(); } } ?>
B<?php class A { public function foo() {} } class B extends A { public function foo() { echo 'Hi'; } } ?>
C<?php class A { public function foo() {} } class B extends A { public function foo() { parent::foo(); } } ?>
D<?php class A { public function foo() {} } class B extends A { public function foo() {} } ?>
Attempts:
2 left
💡 Hint

Check how the parent method is called inside the child method.

🚀 Application
expert
3:00remaining
Determine the number of methods available in child class

Given the following PHP classes, how many methods can be called on an instance of Smartphone?

PHP
<?php
class Device {
    public function powerOn() { return "Powering on"; }
    protected function reset() { return "Resetting"; }
    private function secret() { return "Secret"; }
}

class Phone extends Device {
    public function call() { return "Calling"; }
    protected function reset() { return "Phone reset"; }
}

class Smartphone extends Phone {
    public function browse() { return "Browsing"; }
    private function secret() { return "Smartphone secret"; }
}

$phone = new Smartphone();
A4
B6
C5
D3
Attempts:
2 left
💡 Hint

Count the public methods that can be called directly on a Smartphone instance from outside: powerOn(), call(), browse().