Не могу сообразить почему выводит лишний элемент в массиве
<?php
abstract class RobotParam
{
protected $speed;
protected $height;
protected $weight;
// public $arrayRobot = [];
abstract public function getSpeed();
abstract public function getHeight();
abstract public function getWeight();
}
class Robot1 extends RobotParam
{
protected $speed = 30;
protected $height = 2;
protected $weight = 725;
// public $arrayRobot = [];
public function getSpeed()
{
return $this->speed;
}
public function getHeight()
{
return $this->height;
}
public function getWeight()
{
return $this->weight;
}
}
class Robot2 extends RobotParam
{
protected $speed = 10;
protected $height = 1;
protected $weight = 50;
// public $arrayRobot = [];
public function getSpeed()
{
return $this->speed;
}
public function getHeight()
{
return $this->height;
}
public function getWeight()
{
return $this->weight;
}
}
Главный скрипт:
require_once 'classes/RobotsClass.php';
class FactoryRobot
{
private $cloneRobot1;
private $cloneRobot2;
public $arrayRobot = [];
public function __construct()
{
$this->cloneRobot1 = new Robot1();
$this->cloneRobot2 = new Robot2();
}
public function addType($robotParam)
{
return $robotParam;
}
private function createNewRobots($countRobots, $robotType)
{
for($i = 0; $i < $countRobots; $i++)
{
$this->arrayRobot[$i] = clone $robotType;
}
return $this->arrayRobot;
}
public function createRobot1(int $countRobots)
{
return $this->createNewRobots($countRobots, $this->cloneRobot1);
}
public function createRobot2(int $countRobots)
{
return $this->createNewRobots($countRobots, $this->cloneRobot2);
}
}
$factory = new FactoryRobot();
echo '<pre>';
var_dump($factory->createRobot1(2));
echo '</pre>';
echo '<pre>';
var_dump($factory->createRobot2(1));
echo '</pre>';
Выводит:
array(2) {
[0]=>
object(Robot1)#4 (3) {
["speed":protected]=>
int(30)
["height":protected]=>
int(2)
["weight":protected]=>
int(725)
}
[1]=>
object(Robot1)#5 (3) {
["speed":protected]=>
int(30)
["height":protected]=>
int(2)
["weight":protected]=>
int(725)
}
}
array(2) {
[0]=>
object(Robot2)#6 (3) {
["speed":protected]=>
int(10)
["height":protected]=>
int(1)
["weight":protected]=>
int(50)
}
[1]=> // Этот элемент лишний!!!
object(Robot1)#5 (3) {
["speed":protected]=>
int(30)
["height":protected]=>
int(2)
["weight":protected]=>
int(725)
}
}