@CenterJoin

Как очистить переменную?

Моя фабрика:
Показать код
<?php
interface RobotParam
{
    public function getSpeed();
    public function getHeight();
    public function getWeight();
}

class Robot1 implements RobotParam
{
    private $speed = 30;
    private $height = 2;
    private $weight = 725;

    public function getSpeed() { return $this->speed; }
    public function getHeight() { return $this->height; }
    public function getWeight() { return $this->weight; }
}

class Robot2 implements RobotParam
{
    private $speed = 10;
    private $height = 1;
    private $weight = 50;

    public function getSpeed() { return $this->speed; }
    public function getHeight() { return $this->height; }
    public function getWeight() { return $this->weight; }
}

class FactoryRobot
{
    private $robotName;
    private $arrayRobot = [];

    public function addType($robotParam)
    {
        $this->robotName = $robotParam;
    }

    private function createNewRobots($countRobots, $robotType)
    {
        $this->arrayRobot = [];

        for($i = 0; $i < $countRobots; $i++)
            $this->arrayRobot[$i] = clone $robotType;

        return $this->arrayRobot;
    }

    public function createRobot1(int $countRobots)
    {
        return $this->createNewRobots($countRobots, $this->robotName);
    }

    public function createRobot2(int $countRobots)
    {
        return $this->createNewRobots($countRobots, $this->robotName);
    }
class MergeRobot extends FactoryRobot
{
    private $array = [];

    public function __debugInfo()
    {
        return $this->array;
    }

    public function addRobot($robotType)
    {
        $this->array[] = $robotType;
    }
}
?>

$factory = new FactoryRobot();
$factory->addType(new Robot1());
$factory->addType(new Robot2());

$mergeRobot = new MergeRobot();
$mergeRobot->addRobot(new Robot2());
$mergeRobot->addRobot($factory->createRobot1(1));

echo '<pre>';
var_dump($mergeRobot);
echo '</pre>';


На выходе массив с одинаковыми значениями. Как сделать, чтобы методу $factory->addType не присваивалось только последнее значение new Robot2()?
  • Вопрос задан
  • 175 просмотров
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы