I am getting this error:
uncaught error call to undefine method
Vehicles::setPassengerSeats() in
C:\xampp\htdocs\practice\vehicle.php:91.
I also have added screenshot of the error. Kindly check and tell me how can i solve it ? I think i have problem with my sub class but i don't know where?
Here is my source code:
<?php
class Vehicles{
private $noOfVehicles;
private $color;
private $fuel;
private $speed;
public function getNoOfVehicles(){
return $this->noOfMobiles;
}
public function setNoOfVehicles($Vehicles){
$this->noOfMobiles = $Vehicles;
echo "No of Vehicles are: ".$this->noOfVehicles."</br>";
}
public function getColor(){
return $this->color;
}
public function setColor($look){
$this->color = $look;
echo "</br>The Color of Vehicle is: ".$this->color."</br>";
}
public function getFuel(){
return $this->fuel;
}
public function setFuel($petrol){
$this->fuel = $petrol;
echo "</br>The fuel is: ".$this->color."</br>";
}
public function getSpeed(){
return $this->speed;
}
public function setSpeed($vehicleSpeed){
$this->speed = $vehicleSpeed;
echo "</br>The speed of vehicle is: ".$this->speed."</br>";
}
}
class PassengerVehicles extends Vehicles{
private $passengerSeats;
public function getPassengerSeats(){
return $this->passengerSeats;
}
public function setPassengerSeats($seats){
return $this->passengerSeats = $seats;
echo "</br>Passenger Seats are: ".$this->passengerSeats."</br>";
}
}
class TransportationVehicles extends Vehicles{
private $noOfDoors;
private $loadCapacity;
public function getNoOfDoors(){
return $this->noOfDoors;
}
public function setNoOfDoors($doors){
return $this->noOfDoors = $doors;
echo "</br>The No of Doors are: ".$this->noOfDoors."</br>";
}
public function getLoadCapacity(){
return $this->loadCapacity;
}
public function setLoadCapacity($capacity){
return $this->loadCapacity = $capacity;
echo "The Load Capacity is: ".$this->loadCapacity."</br>";
}
}
$VehiclesObj = new Vehicles;
$VehiclesObj->setNoOfVehicles("15");
$VehiclesObj->setColor("Black");
$VehiclesObj->setFuel("5 Litre");
$VehiclesObj->setSpeed("120 km/h");
$VehiclesObj->setPassengerSeats("4");
$VehiclesObj->setNoOfDoors("4");
$VehiclesObj->setLoadCapacity("500 KG");
?>
You call method setPassengerSeats which is in another class not in Vehicles You should create instance first, then to call this method:
$passangerVehicle = new PassengerVehicles;
$passangerVehicle->setPassengerSeats("4");
You can't call child methods from a parent. You need to create an instance of the child to be able to call parent methods
class Vehicles{
private $noOfVehicles;
private $color;
private $fuel;
private $speed;
public function getNoOfVehicles(){
return $this->noOfMobiles;
}
public function setNoOfVehicles($Vehicles){
$this->noOfMobiles = $Vehicles;
echo "No of Vehicles are: ".$this->noOfVehicles."</br>";
}
public function getColor(){
return $this->color;
}
public function setColor($look){
$this->color = $look;
echo "</br>The Color of Vehicle is: ".$this->color."</br>";
}
public function getFuel(){
return $this->fuel;
}
public function setFuel($petrol){
$this->fuel = $petrol;
echo "</br>The fuel is: ".$this->color."</br>";
}
public function getSpeed(){
return $this->speed;
}
public function setSpeed($vehicleSpeed){
$this->speed = $vehicleSpeed;
echo "</br>The speed of vehicle is: ".$this->speed."</br>";
}
}
class PassengerVehicles extends Vehicles{
private $passengerSeats;
public function getPassengerSeats(){
return $this->passengerSeats;
}
public function setPassengerSeats($seats){
return $this->passengerSeats = $seats;
echo "</br>Passenger Seats are: ".$this->passengerSeats."</br>";
}
}
class TransportationVehicles extends Vehicles{
private $noOfDoors;
private $loadCapacity;
public function getNoOfDoors(){
return $this->noOfDoors;
}
public function setNoOfDoors($doors){
$this->noOfDoors = $doors;
echo "</br>The No of Doors are: {$this->noOfDoors}</br>";
return $this->noOfDoors;
}
public function getLoadCapacity(){
return $this->loadCapacity;
}
public function setLoadCapacity($capacity){
return $this->loadCapacity = $capacity;
echo "The Load Capacity is: ".$this->loadCapacity."</br>";
}
}
$truck = new TransportationVehicles();
$truck->setNoOfVehicles("15");
$truck->setColor("Black");
$truck->setFuel("5 Litre");
$truck->setSpeed("120 km/h");
$truck->setNoOfDoors("4");
$truck->setLoadCapacity("500 KG");
$taxi = (new PassengerVehicles())->setPassengerSeats('4');
In this case, you will have two instances of the Vehicles class + own child.
The first instance is related to the Vehicle itself + transport properties like $noOfDoorsand $loadCapacity - a truck for example.
The second is an instance of a passenger based vehicle - taxi for example.
And you tried to get passengers option of a taxi from a bus.
Related
I have a problem with an error I am getting that says:
Class Car contains 1 abstract method and must therefore be decla
red abstract or implement the remaining methods (Car::accelerate) in C:\xampp
\htdocs\php\learn_php_oop\Car.php on line 58.
This is the code in two files I am using:
Car.php
<?php
/**
* represents generic properties and methods for any type of car
*/
class Car
{
protected $colour, $doorNumber, $fuelType, $rightHandDrive, $accelerate;
public function __construct($rightHandDrive = true)
{
$this->rightHandDrive = $rightHandDrive;
}
public function getColour()
{
return $this->colour;
}
public function setColour($colour)
{
$this->colour = $colour;
}
public function getDoorNumber()
{
return $this->doorNumber;
}
public function setDoorNumber($doorNumber)
{
$this->doorNumber = $doorNumber;
}
public function getFuelType()
{
return $this->fuelType;
}
public function setFuelType($fuelType)
{
$this->fuelType = $fuelType;
}
public function getRightHandDrive()
{
return $this->rightHandDrive;
}
public function setRightHandDrive($rightHandDrive)
{
$this->rightHandDrive = $rightHandDrive;
}
abstract protected function accelerate();
}
?>
Sport_car.php
<?php
include ('Car.php');
/**
* represents sport cars
*/
class Sport_car extends Car
{
public function accelerate()
{
$this->accelerate = 5;
}
}
?>
I have spent some time trying to figure out why this is happening but I just do not know why? Please help.
It's an OOP problem, in your case you must declare your Car Object as Abstract like this :
<?php
/**
* represents generic properties and methods for any type of car
*/
abstract class Car
{
protected $colour, $doorNumber, $fuelType, $rightHandDrive, $accelerate;
public function __construct($rightHandDrive = true)
{
$this->rightHandDrive = $rightHandDrive;
}
public function getColour()
{
return $this->colour;
}
public function setColour($colour)
{
$this->colour = $colour;
}
public function getDoorNumber()
{
return $this->doorNumber;
}
public function setDoorNumber($doorNumber)
{
$this->doorNumber = $doorNumber;
}
public function getFuelType()
{
return $this->fuelType;
}
public function setFuelType($fuelType)
{
$this->fuelType = $fuelType;
}
public function getRightHandDrive()
{
return $this->rightHandDrive;
}
public function setRightHandDrive($rightHandDrive)
{
$this->rightHandDrive = $rightHandDrive;
}
abstract protected function accelerate();
}
?>
Explanations :
A class wich is extended with at least one abstract method in it has to be defined as abstract itself, otherwise you'll get an error
I am having a PHP interface coding problem. I took java code from the book “Head First Design Patterns and converted it to the code below. I am using MAMP/ PHP 5.6.2 and NetBeans 8.1.
I am trying to implement an interface “TestInterface” in the Menu class that extends an abstract class (MenuComponent). The Menu class will not start with the “TestInterface” implementation. The code runs when I comment out “TestInterface" in the Menu class declaration as the code below. And while “TestInterface” is commented out, PHP throws no errors even when declaring the interface and keeping the interface function as a Menu member function. I have successfully ran simpler code while extending and implementing at the same time using the same platform as stated above. Because of success with simpler code, I believe there is structural or syntax error in my code below. I hoping that someone can help me find what I am doing wrong. Thanks in advance.
<?php
$run = new myclass;
$run->main();
class myclass {
private $pancakeHouseMenu;
private $allMenus;
private $waitress;
public function main(){
echo "<br />hi main!<br />";
$this->pancakeHouseMenu = new Menu("PANCAKE HOUSE MENU", "Breakfast");
$this->allMenus = new Menu("ALL MENUS", "All menus combind");
$this->allMenus->add($this->pancakeHouseMenu);
$this->pancakeHouseMenu->add(new MenuItem(
"Regular Pancake Breakfast",
"Pancakes with eggs and sausage"));
$this->waitress = new Waitress($this->allMenus);
$this->waitress->printMenu();
}
}
interface TestInterface {
public function interfaceTest();
}
abstract class MenuComponent {
public function add(MenuComponent $newMenuComponent) {
throw new InvalidArgumentException("Exception thrown");
}
public function getName() {
throw new InvalidArgumentException("Exception thrown");
}
public function getDescription() {
throw new InvalidArgumentException("Exception thrown");
}
public function printOut() {
throw new InvalidArgumentException("Exception thrown");
}
}
class Waitress {
private $allMenus;
public function __construct(MenuComponent $allMenus) {
$this->allMenus = $allMenus;
$this->allMenus->add($allMenus);
}
public function printMenu() {
$this->allMenus->printOut();
}
}
class MenuItem extends MenuComponent {
private $name;
private $description;
public function __construct($name, $description) {
$this->name = $name;
$this->description = $description;
}
public function getName() {
return $this->name;
}
public function getDescription() {
return $this->description;
}
public function printOut() {
print(" " . $this->getName());
print(" -- " . $this->getDescription());
}
}
class Menu extends MenuComponent /*** implements TestInterface ***/ {
private $menuComponents = array();
private $name;
private $description;
// private $testVar;
public function __construct($name, $description) {
$this->name = $name;
$this->description = $description;
$this->testVar = "Interface test succeeded";
}
public function interfaceTest(){
return $this->testVar;
}
public function add(MenuComponent $newMenuComponent) {
array_push($this->menuComponents, $newMenuComponent);
}
public function getName() {
return $this->name;
}
public function getDescription() {
return $this->description;
}
public function printOut() {
print("<br />" . $this->getName());
print(", " . $this->getDescription());
print("<br />---------------------");
print("<br />Testing interface var: ". $this->interfaceTest());
}
}
?>
In your code you create an object above the declaration of your classes. This seems to be ok if your classes do not implement any interfaces. Since your class menu does implement the interface TestInterface, PHP does not accept your object instantiation before the declaration of your classes.
The solution is quite simple, place your object creation of myclass below the object declaration:
<?php
class myclass {
private $pancakeHouseMenu;
private $allMenus;
private $waitress;
...
public function getDescription() {
return $this->description;
}
public function printOut() {
print("<br />" . $this->getName());
print(", " . $this->getDescription());
print("<br />---------------------");
print("<br />Testing interface var: ". $this->interfaceTest());
}
}
$run = new myclass;
$run->main();
?>
I have a person class with a $name property and an officer class with an $officer_name property. I need to be able to get the $name property from the person class to the officer class. In this case it should have an output of "Major Blake". Do I need to make an instance of the officer class for me to be able to echo this out?
class person {
protected $name;
private function set_name($fv_name) {
$this->name = $fv_name;
}
public function get_name() {
return $this->name;
}
function __construct($fv_name) {
$this->set_name($fv_name);
}
}
class officer extends person {
private function give_rank(){
return "Major ";
}
function __construct() {
echo $officer_name = $this->give_rank() . parent::get_name();
}
}
$iv_person = new person("Blake");
Please try this code :
your class :
<?php
class person {
protected $name;
private function set_name($fv_name) {
$this->name = $fv_name;
}
public function get_name() {
return $this->name;
}
function __construct($fv_name) {
$this->set_name($fv_name);
}
}
class officer extends person {
private function give_rank(){
return "Major ";
}
function __construct($fv_name) {
parent::__construct($fv_name);
}
function output(){
return $this->give_rank().parent::get_name();
}
}
your call :
$iv_person = new officer("Blake");
echo $iv_person->output();
The following code taken from a 2010 PHP book I a m currently reading returns a "Fatal error: Call to a member function getPrice() on a non-object in Z:\home\different-tasks\www\cardecorator.php on line 15" How come that a car is not an object?
<?php
abstract class AbstractCar {
public abstract function getPrice();
public abstract function getManufacturer();
};
class Car extends AbstractCar {
private $price = 16000;
private $manufacturer = 'Acme Autos';
public function getPrice() { return $this->price; }
public function getManufacturer() { return $this->manufacturer; }
};
class CarDecorator extends AbstractCar {
private $target;
function __construct( Car $target ) { $this->target = $target; }
public function getPrice() { return $target->getPrice(); }
public function getManufacturer() { return $target->getManufacturer(); }
};
class NavigationSystem extends CarDecorator {
public function getPrice() { return parent::getPrice()+1000; }
};
$car = new Car();
$car = new NavigationSystem( $car );
//$car = new LeatherSeats( $car );
echo $car->getPrice();
public function getPrice() { return $target->getPrice(); }
should be
public function getPrice() { return $this->target->getPrice(); }
you have several mistakes like this there
I went on to download the examples from the publisher's site, and the downloadsable code works. The author explicitly mentioned in the preface that some elements of the examples given in the printed version might be missing to provide conciseness.
So,I guess that I should study the code that is downloaded rather than what is printed.
I have this code and the sad thing is i can't change anything or add outside the class STUDENT. I can modify only inside STUDENT and i can't alter the private fields. However I need to display the value of the field $nume in red color. Ideas?
class STUDENT {
private $nume,$prenume;
// Constructor
public function __construct($nume , $prenume){
$this->nume=$nume;
$this->prenume=$prenume;
}
public function __toString(){
return $this->nume.".".$this->prenume;
}
}
$student = new STUDENT("mr","Jack");
echo "student: ". $student ."<hr/>";
You can make the properties public so you can access them from the outside like this:
class STUDENT
{
public $nume;
public $prenume;
// Constructor
public function __construct($nume , $prenume)
{
$this->nume=$nume;
$this->prenume=$prenume;
}
public function __toString()
{
return $this->nume.".".$this->prenume;
}
}
$student = new STUDENT("mr","Jack");
echo "<span style='color:red'>student: ". $student->nume ."</span><hr/>";
Or if you need to keep the private you can create a function inside the class to output it:
class STUDENT
{
private $nume;
private $prenume;
// Constructor
public function __construct($nume , $prenume)
{
$this->nume=$nume;
$this->prenume=$prenume;
}
public function __toString()
{
return $this->nume.".".$this->prenume;
}
public function displayNume()
{
echo "<span style='color:red'>student: ". $this->nume ."</span><hr/>";
}
}
Which you can then access like this:
$student = new STUDENT("mr","Jack");
$student->displayNume();
Have you tried...
public function __toString(){
$red = '<span style="color: red;">' . $this->nume . '</span>';
return $red.".".$this->prenume;
}
?