使用函数更改php类的值

时间:2014-06-17 08:01:40

标签: php function class

我想要做的是,更改以下变量将给出的值。例如我有汽车1并且它的前三个属性将被修复,例如汽车的种类,颜色及其ID /名称但我想要做的是给出一个其他值的列表,每当页面刷新时,盔甲,轮子,引擎和门都可以随机更改。我会把我的代码放在下面谢谢,如果我不清楚,对不起,我将列出每个随机值可以是什么,所以请任何帮助将不胜感激!谢谢!

<?php

class car_1
{
    //fixed//
    public $id;
    public $color;
    public $type;
    //fixed//

    //need these to change//
    public $armour;
    public $wheels;
    public $engine;
    public $doors;


    public function __construct($id, $colour, $type, $armour, $wheels, $engine, $doors)
    {
        $this->id     = $id;
        $this->color  = $colour;
        $this->type   = $type;
        $this->armour = $armour;
        $this->wheels = $wheels;
        $this->engine = $engine;
        $this->doors  = $doors;
    }


    public function Action()
    {
        return "I'am ".

        $this->id."<br> My colour is ".
        $this->color."<br> My Armour is at = ".
        $this->armour."<br> I'am a ".
        $this->type." car. <br> I have ".
        $this->wheels." Wheels.<br> My Engine level is at ".
        $this->engine."<br> I have ".
        $this->doors." doors";


    }
}


$car1 = new car_1('Car 1', 'Red', 'Sports', '60%', '4', '2500', '3');
$car2 = new car_1('Car 2', 'Blue', 'Convertible', '70%', '4', '1500', '5');
$car3 = new car_1('Car 3', 'Green', 'Sports', '50%', '4', '1800', '3');
$car4 = new car_1('Car 4', 'Yellow', 'Off-Road', '60%', '3', '1300', '3');
$car5 = new car_1('Car 5', 'Orange', 'Saloon', '80%', '4', '2000', '5');
$car6 = new car_1('Car 6', 'Gold', 'Super', '100%', '4', '2000', '5');

echo "<p class=\"red\">".$car1->Action()."</p>";
echo "<p class=\"blue\">".$car2->Action()."</p>";
echo "<p class=\"green\">".$car3->Action()."</p>";
echo "<p class=\"yellow\">".$car4->Action()."</p>";
echo "<p class=\"black\">".$car5->Action()."</p>";
echo "<p class=\"gold\">".$car6->Action()."</p>";

?>

1 个答案:

答案 0 :(得分:0)

看起来你需要这个,而不是你所描述的:

$my_array = array("a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "0", "1", "2", "3", "4", "5");
$car1->armour = array_rand($my_array);
相关问题