<?php
class Vehicle{
protected $brand;
function __construct($brand){
$this->setBrand($brand);
}
function setBrand($brand){
$this->brand=$brand;
}
function getBrand(){
return $this->brand;
}
function honk(){
echo "Beep Beep Beep...";
}
}
class Car extends Vehicle{
private $modelName;
function setModelName($modelName){
$this->modelName=$modelName;
}
function getModelName(){
return $this->modelName;
}
}
$carObject = new Car("Lamborghini");
echo "</br>Car brand is ".$carObject->getBrand()."<br>";
$carObject->setModelName("Aventador");
echo "Car model is ".$carObject->getModelName()."<br>";
echo($carObject->getBrand()." ".$carObject->getModelName()." is ".$carObject->honk());
?>
I added this function to subclass. but i do not understand that function too.
function honk(){
parent::honk();
}
The output that i want is: Lamborghini Aventador is Beep Beep Beep…
but i am getting Beep Beep Beep…Lamborghini Aventador is
2
Answers
Your honk
echo
runs first and then the last echo runs. Perhaps you need toreturn
the message instead ofecho
?In this line:
The method calls are evaluated one after:
$carObject->getBrand()
is called, returning the stringLamborghini
.$carObject->getModelName()
is called, returning the stringAventador
.$carObject->honk()
is called, echoing the stringBeep Beep Beep...
to the output. It does not return anything.Thus leading to
Beep Beep Beep...Lamborghini Aventador is
.You may have meant to
return
inhonk()
: