I've got a class that I want to make immutable but that class has a lot of properties.
<?php
class Gameworld {
/** @var string */
private $name;
/** @var string */
private $type;
/** @var bool */
private $is_online;
/** @var int */
private $online_players;
/** @var int */
private $online_players_record;
/** @var string */
private $description;
/** @var string */
private $location;
/** @var \DateTime */
private $created_at;
}
How to create such object? When I introduce public function __construct()
with all those properties it will get bloated. If I introduce setters it won't be immutable anymore.
EDIT: I was thinking about making setters but ones that could be only used once. Thanks to that I won't have bloated constructor but for some reason it seems not like a good idea. Something like:
class Gameworld {
... old properties ...
/** @var array */
private $used_setters = [];
public function setName(string $name){
if(in_array('name', $this->used_setters)){
throw new ImmutableException('Class Gameworld is immutable.');
}
$this->name = $name;
$this->used_setters[] = 'name';
}
}