Example #1
0
//$cars[0]->km = $cars[0]->km + 30; 
$cars[0]->move(30);
//$cars[1]->km = $cars[1]->km + 30; 
$cars[1]->move(20);
echo "<pre>";
print_r($cars);
 * 
 * */
// public / private / protected
require_once "car.class.php";
require_once "truck.class.php";
require_once "plane.class.php";
$car = new Car(20, 'red', 5, 2);
$car->unloadPass(1);
$truck = new Truck(50, 'yello', 2200);
$truck->load(10);
$truck->unload(1000);
$vehicle = new Vehicle(20, 'blue');
$plane = new Plane(120, 'grey', 'AIRsoft', 1000, 8000);
$plane->drop(200);
echo "<pre>";
print_r($car);
print_r($truck);
print_r($vehicle);
$vehicle = new Vehicle(20, 'red');
$vehicle->move(20);
$vehicle->move(30);
$vehicle->move(15);
echo "<pre>";
print_r($vehicle);
Example #2
0
    {
        $this->type = $type;
    }
    public function showAuto()
    {
        parent::showAuto();
        echo "<div style='display:block; padding:20px; background:" . $this->color . "'><br>" . $this->capacity . " kg</div>";
    }
}
echo '<h1>Class Car</h1><hr>';
$variable = "Car";
$car = new $variable(200, 30);
echo $car->getCoords();
$car->move(400, 300);
echo $car->getCoords();
echo $car->showAuto();
if (method_exists($car, "setType")) {
    echo 'Method setType exist<br>';
}
if (property_exists($car, "x")) {
    echo 'property x exist<br>';
}
if (class_exists("Car")) {
    echo 'class Car exist<br>';
}
echo '<hr>';
echo '<h1>Class Truck</h1><hr>';
$truck = new Truck(200, 30, "Truck", 120, "#00ff00", 3000);
$truck->move(200, 100);
echo $truck->showAuto();
echo '<hr>';
Example #3
0
$car2->move(15);
print_r($car2);
*/
/*
$car->brand = "VW";
$car->color = 'Purple';
$car->seats = 5;
$car->doors = 3;
$car->year = 1991;
$car->price = 500;
*/
/*
$car2 = new automobile;

$car2->brand = "Audi";
$car2->color = 'Rusa';
$car2->seats = 2;
$car2->doors = 3;
$car2->year = 1999;
$car2->price = 4000;
*/
require_once "truck.php";
require_once "car.php";
$x = new Truck(5000, 50, 'Blue');
$x->move(100);
$x->move(50);
echo "<pre>";
print_r($x);
$car = new Car(5, 20, 'White');
echo "<pre>";
print_r($car);
Example #4
0
<?php

/**
 * Copyright (c) 2014 Keith Casey
 *
 * This code is designed to accompany the lynda.com video course "Design Patterns in PHP"
 *   by Keith Casey. If you've received this code without seeing the videos, go watch the
 *   videos. It will make way more sense and be more useful in general.
 */
include_once 'vehicle.php';
$car = new Car(4);
echo $car->getType();
echo '<br />';
$truck = new Truck(18);
echo $truck->getType();
{
    public function __construct($x = 0, $y = 0)
    {
        parent::__construct($x, $y);
    }
    //это делать не обязательно берет из родительского конструктора
    public function move($x, $y)
    {
        $this->strMove($x, $y, "легковой");
    }
}
class Truck extends Auto
{
    private $capasity;
    public function __construct($x = 0, $y = 0, $capasity = 5000)
    {
        parent::__construct($x, $y);
        $this->capasity = $capasity;
    }
    //это делать не обязательно берет из родительского конструктора
    public function move($x, $y)
    {
        $this->strMove($x, $y, "грузовой");
    }
}
$auto = new Auto();
$car = new Car();
$truck = new Truck();
$auto->move(10, 10);
$car->move(10, 10);
$truck->move(10, 10);
Example #6
0
<?php
class Car {
    public $speed = 0; //汽车的起始速度是0

    public function speedUp() {
        $this->speed += 10;
        return $this->speed;
    }
}
//定义继承于Car的Truck类
class Truck extends Car {
    //覆盖speedUp方法
    public function speedUp() {
        $this->speed = parent::speedUp() + 50;
    }
}

$car = new Truck();
$car->speedUp();
echo $car->speed;
?>
Example #7
0
//访问对象成员
$car->name = '奥迪A6';
echo $car->getName();
Car::echoColor();
var_dump($car);
unset($car);
//继承
class Truck extends Car
{
    public function rename()
    {
        //调用父类成员
        $this->name = parent::getName() . '2';
    }
}
$truck = new Truck();
$truck->rename();
echo $truck->getName();
//对象的成员可以动态添加,修改,删除
$truck->long = 3;
echo $truck->long;
//读取cookie
var_dump($_COOKIE['TestCookie']);
//设置cookie,指定字段名,值,有效期,有效路径,域
setcookie("TestCookie", 1, time() + 3600, "/");
//删除cookie
setcookie('test', '', time() - 1);
//开启session
session_start();
//设置session字段
$_SESSION["time"] = time();
Example #8
0
//$cars[0]->km = $cars[0]->km + 30; 
$cars[0]->move(30);
//$cars[1]->km = $cars[1]->km + 30; 
$cars[1]->move(20);
echo "<pre>";
print_r($cars);
 * 
 * */
// public / private / protected
require_once "car.class.php";
require_once "truck.class.php";
require_once "plane.class.php";
$car = new Car(20, 'red', 5);
$car->load(3);
$car->unload(1);
$truck = new Truck(50, 'yello', 2200);
$truck->load(2);
$truck->unload(5);
$vehicle = new Vehicle(20, 'blue');
$plane = new Plane(3000, "white", 200, 5000, "Emirates", 40);
$plane->attack(70);
echo "<pre>";
print_r($car);
print_r($truck);
print_r($vehicle);
print_r($plane);
if ($plane->seats >= 200) {
    echo "No Seats Available";
}
$vehicle = new Vehicle(20, 'red');
$vehicle->move(20);