Beispiel #1
0
    {
        return $this->status;
    }
    //Setter - for private - status
    function setStatus($new_status)
    {
        $this->status = $new_status;
    }
}
$car1 = new Car("2014 Porsche 911", 114991, 0, NULL, "images/porsche.jpg");
$car2 = new Car("2011 Ford F450", 55995, 0, NULL, "images/ford.jpg");
$car3 = new Car("2013 Lexus RX 350", 44700, 0, NULL, "images/lexus.jpg");
$car4 = new Car("Mercedes Benz CLS550", 39900, 37979, NULL, "images/mercedes.jpg");
$car5 = new Car("Ford F150", 24900, 45979, NULL, "images/f150.jpg");
// error testing: var_dump("Car 4 status " . $car4->getStatus());
$car4->setStatus("used");
// error testing: var_dump("Car 4 status " . $car4->getStatus());
$cars = array($car1, $car2, $car3, $car4, $car5);
$cars_matching_search = array();
foreach ($cars as $car) {
    if ($car->getPrice() < $_GET["price"]) {
        array_push($cars_matching_search, $car);
    }
}
?>

<!DOCTYPE html>
<html>
<head>
    <link rel="stylesheet" href="styles.css">
    <title>Your Car Dealership's Homepage</title>
Beispiel #2
0
// all objects in PHP are based on classes
class Car
{
    // variables inside a class are called properties or attributes
    public $make = null;
    public $model = null;
    public $color = null;
    protected $status = 'off';
    // functions inside a class are called methods
    public function setStatus($value)
    {
        // '$this' refers to the current instance
        $this->status = $value;
    }
    public function getStatus()
    {
        return $this->status;
    }
}
// we create instances (actual objects) of a class using 'new'
$yourCar = new Car();
$myCar = new Car();
// use the '->' operator to access an object's members
$yourCar->color = 'red';
$yourCar->color = 'blue';
echo '<p>Your car color is ' . $yourCar->color . '.</p>';
//echo $yourCar->status;
echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>';
$yourCar->setStatus('running');
echo '<p>Your car status is ' . $yourCar->getStatus() . '</p>';
echo '<p>My car status is ' . $myCar->getStatus() . '</p>';