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();

?>

Recommended Answers

All 2 Replies

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;
}

thank you very much for this, very handy.

regards

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.