0

Hi All,

Im after some advice on extending classes.

ive got a vehicle class, which is extended by the motorcycle class.

my question is how do i create a class of the motor cycle that includes the vehicle class elements.

<?php

class vehicle {
	
	private $wheels;
	private $engineType;
	private $engineSize;
	private $color;
	
	function __construct($wheels, $engineType, $engineSize, $color){
		
		$this->wheels = $wheels;
		$this->engineType = $engineType;
		$this->engineSize = $engineSize;
		$this->color = $color;
		
		}
	public function getWheels(){
		return $this->wheels;
	}
	
}


class motorcycle extends vehicle {
 private $sidecar;
 private $handlebars;
 
 	function __construct($sidecar, $handlebars){
		$this->sidecar = $sidecar;
		$this->handlebars = $handlebars;
	}
	public function getSideCar(){
		return $this->sidecar;
	}
}


$vehicle = new vehicle (2, 1500, 1.2, "red");
echo $vehicle->getWheels();
echo "<br>";
echo $vehicle->test();

?>
2
Contributors
2
Replies
3
Views
7 Years
Discussion Span
Last Post by whiteyoh
0

Your __construct function would have to include those parameters. Then within it you can call the parent's constructor:

function __construct($wheels, $engineType, $engineSize, $color, $sidecar, $handlebars)
{
  parent::__construct($wheels, $engineType, $engineSize, $color);
  $this->sidecar = $sidecar;
  $this->handlebars = $handlebars;
}

Edited by pritaeas: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.