Vehicle with a method getType().Car and Bike that extend Vehicle and override getType().printVehicleType that accepts a parameter type hinted as Vehicle.getType() inside printVehicleType and print the result.Vehicle with a method getType().Car and Bike that extend Vehicle and override getType().printVehicleType that accepts a parameter type hinted as Vehicle.getType() inside printVehicleType and print the result.Vehicle with a public method getType() that returns the string 'Generic Vehicle'.Use class Vehicle {} and inside it define public function getType(): string that returns 'Generic Vehicle'.
Car and Bike that extend Vehicle. Override the getType() method in each to return 'Car' and 'Bike' respectively.Use class Car extends Vehicle and override getType() to return 'Car'. Do the same for Bike returning 'Bike'.
printVehicleType that accepts one parameter $vehicle type hinted as Vehicle. Inside the function, call $vehicle->getType() and print the returned string.Define function printVehicleType(Vehicle $vehicle): void and inside use echo $vehicle->getType() to print the type.
$car of class Car and an object $bike of class Bike. Call printVehicleType with $car and then with $bike to print their types.Create objects with new Car() and new Bike(). Then call printVehicleType($car) and printVehicleType($bike).
What does type hinting with a parent class in PHP allow?
For example, using function foo(ParentClass $obj).
ParentClass $obj allows passing instances of ParentClass or any class that extends it.Which of the following is the correct syntax to type hint a function parameter with a parent class Animal in PHP?
function feed(____ $pet) { /* ... */ }Type $variable.Animal $pet.Given the code below, what will be the output?
class Vehicle {}
class Car extends Vehicle {}
function startEngine(Vehicle $v) {
echo get_class($v);
}
$car = new Car();
startEngine($car);What is the error in the following PHP code?
class Fruit {}
class Apple extends Fruit {}
function eat(Apple $fruit) {
echo "Eating apple";
}
$fruit = new Fruit();
eat($fruit);Consider the following classes and function:
class Employee {}
class Manager extends Employee {}
class Developer extends Employee {}
function assignTask(Employee $e) {
if ($e instanceof Manager) {
return "Assigning management task";
} elseif ($e instanceof Developer) {
return "Assigning coding task";
} else {
return "Assigning general task";
}
}
$worker = new Manager();
echo assignTask($worker);What will be the output and why?
Pass the test and this pattern moves unknown