Complete the code to define a class that inherits from another class.
<?php class Animal { public function sound() { echo "Some sound"; } } class Dog extends [1] { } $dog = new Dog(); $dog->sound(); ?>
extends.extends keyword.The Dog class inherits from the Animal class using the extends keyword.
Complete the code to override the method in the child class.
<?php class Animal { public function sound() { echo "Some sound"; } } class Cat extends Animal { public function [1]() { echo "Meow"; } } $cat = new Cat(); $cat->sound(); ?>
The child class overrides the sound method to provide its own implementation.
Fix the error in the code to call the parent class method inside the child class method.
<?php class Bird { public function sound() { echo "Chirp"; } } class Parrot extends Bird { public function sound() { [1]::sound(); echo " and Talk"; } } $parrot = new Parrot(); $parrot->sound(); ?>
self or class name instead of parent to call the parent method.$this which calls the current class method causing recursion.Use parent::sound() to call the parent class method inside the child class.
Fill both blanks to create a class that inherits and adds a new method.
<?php class Vehicle { public function start() { echo "Starting vehicle"; } } class Car [1] Vehicle { public function [2]() { echo "Driving car"; } } $car = new Car(); $car->start(); $car->drive(); ?>
implements instead of extends for class inheritance.The Car class inherits from Vehicle using extends and adds a new method drive.
Fill all three blanks to create a class that inherits, overrides a method, and calls the parent method.
<?php class Computer { public function boot() { echo "Booting computer"; } } class Laptop [1] Computer { public function [2]() { [3]::boot(); echo " with battery"; } } $laptop = new Laptop(); $laptop->boot(); ?>
The Laptop class inherits from Computer using extends, overrides the boot method, and calls the parent method with parent::boot().