Complete the code to make class B inherit from class A.
<?php class A { public function greet() { return "Hello from A"; } } class B [1] A { } $b = new B(); echo $b->greet(); ?>
In PHP, to make one class inherit from another, use the extends keyword.
Complete the code to call the parent class constructor inside the child class constructor.
<?php class ParentClass { public function __construct() { echo "Parent constructor called\n"; } } class ChildClass extends ParentClass { public function __construct() { [1]; echo "Child constructor called\n"; } } new ChildClass(); ?>
To call the parent class constructor in PHP, use parent::__construct().
Fix the error in the code to properly override the greet method and call the parent greet method.
<?php class Base { public function greet() { return "Hello from Base"; } } class Derived extends Base { public function greet() { return [1] . " and Derived"; } } $d = new Derived(); echo $d->greet(); ?>
To call a parent class method in PHP, use parent::methodName().
Fill both blanks to create a child class that overrides a method and calls the parent method inside it.
<?php class Animal { public function sound() { return "Some sound"; } } class Dog [1] Animal { public function sound() { return [2] . " Woof!"; } } $dog = new Dog(); echo $dog->sound(); ?>
The child class uses extends to inherit. Inside the overridden method, parent::sound() calls the parent's method.
Fill all three blanks to define a child class that extends a parent, overrides a method, and calls the parent's method with additional text.
<?php class Vehicle { public function description() { return "This is a vehicle"; } } class Car [1] Vehicle { public function description() { return [2] . [3]; } } $car = new Car(); echo $car->description(); ?>
The class uses extends to inherit. The method calls parent::description() and adds extra text.