示例#1
0
<?php

include 'Car.php';
$c1 = new Car('honda');
// create $c1 object
$c2 = new Car('toyota');
// create $c2 object
$c1->setColor('red');
$c2->setColor('blue');
echo $c1->getColor();
echo '<br>';
echo $c2->getColor();
示例#2
0
<?php

include 'classes/car.php';
//using public properties
$car1 = new Car();
// $car1 -> make = 'Toyota';
// echo $car1 -> make.'<br />';
//
// $car1 -> make = 'Toyota';
// $car1 -> make = 'Honda';
// echo $car1 -> make.'<br />';
//using a public method
$car1->start();
$car1->setMake('Chevy');
echo $car1->getMake() . '<br />';
//constructors
// $car2 = new Car;
$car2 = new Car('Honda', 'Accord', 'Red');
echo $car2->getColor() . '<br />';
$car3 = new Car('Honda', 'Civic', 'Blue');
echo $car3->getColor() . '<br />';
echo "<p>Person: " . $bmw->buyCar() . "</p>";
// ---------Nissan  sports car
echo "<hr>";
$nissanSport = Car::withPriceandMaxspeed("Nissan", false, "green", 200000, 320);
//$model, $isSportsCar, $color, $price, $maxSpeed
var_dump($nissanSport);
echo "<p> New sports car constructor used: Nissan : " . $nissanSport->getModel() . " </p>";
echo "<p> Sports car? : " . $nissanSport->getIsSportscar() . "</p>";
echo "<p> Sports car speed : " . $nissanSport->getMaxspeed() . "</p>";
$priceNissanforscrap = $nissanSport->calculateCarPriceForScrap(200);
echo "<p> The price for scrap is : " . $priceNissanforscrap . " </p>";
echo "<hr>";
//-------------------------------------------------------------------------------//
//Second Car :Mercedez
$merc = new Car("Voldemor", "mercedez-benz", "220", "silver", false);
echo "<h3>Car 2 (merc) properties: " . $merc->getColor() . "</h3>";
echo "<p>" . $merc->getModel() . "</p>";
echo "<p>" . $merc->getMaxspeed() . "</p>";
echo "<p>" . $merc->getcurrentSpeed() . "</p>";
echo "<p>" . $merc->getColor() . "</p>";
echo "<p>" . $merc->getCurrentGear() . "</p>";
//Show old speed
echo "<p> Old Speed: " . $merc->getcurrentSpeed() . " </p>";
//Change the speed
$merc->setcurrentSpeed(60);
//Show new speed
echo "<p>Changed Speed: </p>";
echo "<p>" . $merc->getcurrentSpeed() . "</p>";
//Accelerate
echo "<p>Accelerated speed : " . $merc->accelerate() . "</p> ";
//Show old gear
示例#4
0
include "Vehicle.php";
class Car extends Vehicle
{
    private $noOfDoors;
    private $color;
    private $brand;
    public function __construct($cc, $hp, $noOfDoors, $color, $brand)
    {
        echo "<br/>Hi from car constructor";
        parent::__construct('Car', $cc, $hp);
        $this->noOfDoors = $noOfDoors;
        $this->color = $color;
        $this->brand = $brand;
    }
    public function getColor()
    {
        return $this->color;
    }
    public function getNoOfDoors()
    {
        return $this->noOfDoors;
    }
    public function getBrand()
    {
        return $this->brand;
    }
}
$v = new Car(1.3, 100, 4, 'Golden', 'BMW');
echo "<br/>Type is " . $v->getType() . ", CC : " . $v->getCc() . ", HP : " . $v->getHp() . ", Color : " . $v->getColor() . ", Brand : " . $v->getBrand();