A setter method can validate or normalize a value before storing it.

controlled write Setters keep direct writes out of the public API while still allowing updates.

Setter Method

target
setter_method.php
Replay: real traced execution (multi-file project)
<?php
class Thermostat
{
    private int $temperature = 70;

    public function setTemperature(int $temperature): void
    {
        $this->temperature = max(50, min(90, $temperature));
    }

    public function temperature(): int
    {
        return $this->temperature;
    }
}

$target = 72;
$thermostat = new Thermostat();
$thermostat->setTemperature($target);
$stored = $thermostat->temperature();

echo "target=" . $target . "\n";
echo "stored=" . $stored . "\n";
<?php
class Thermostat
{
    private int $temperature = 70;

    public function setTemperature(int $temperature): void
    {
        $this->temperature = max(50, min(90, $temperature));
    }

    public function temperature(): int
    {
        return $this->temperature;
    }
}

$target = 45;
$thermostat = new Thermostat();
$thermostat->setTemperature($target);
$stored = $thermostat->temperature();

echo "target=" . $target . "\n";
echo "stored=" . $stored . "\n";
<?php
class Thermostat
{
    private int $temperature = 70;

    public function setTemperature(int $temperature): void
    {
        $this->temperature = max(50, min(90, $temperature));
    }

    public function temperature(): int
    {
        return $this->temperature;
    }
}

$target = 95;
$thermostat = new Thermostat();
$thermostat->setTemperature($target);
$stored = $thermostat->temperature();

echo "target=" . $target . "\n";
echo "stored=" . $stored . "\n";
  1. $target ← 72, $thermostat ← Thermostat

    17$target→ 72 = 72; //@target=45, 9518$thermostat→ Thermostat = new Thermostat();19$thermostatThermostat->setTemperature($target72);20$stored = $thermostat->temperature();
  2. $this->temperature ← 72

    6public function setTemperature(int $temperature72): void7{8    $this->temperature→ 72 = max(50, min(90, $temperature72));9}
    values this stepThermostat$this
  3. $thermostat->setTemperature($target);

    18$thermostat = new Thermostat();19$thermostatThermostat->setTemperature($target72);20$stored = $thermostatThermostat->temperature();
  4. public function temperature(): int

    11public function temperature(): int12{13    return $this->temperature72;14}
  5. $stored ← 72

    19$thermostat->setTemperature($target);20$stored→ 72 = $thermostatThermostat->temperature();2122echo "target=" . $target72 . "\n";23echo "stored=" . $stored72 . "\n";
    outputtarget=72
    stored=72
  1. $target ← 45, $thermostat ← Thermostat

    17$target→ 45 = 45;18$thermostat→ Thermostat = new Thermostat();19$thermostatThermostat->setTemperature($target45);20$stored = $thermostat->temperature();
  2. $this->temperature ← 50

    6public function setTemperature(int $temperature45): void7{8    $this->temperature→ 50 = max(50, min(90, $temperature45));9}
    values this stepThermostat$this
  3. $thermostat->setTemperature($target);

    18$thermostat = new Thermostat();19$thermostatThermostat->setTemperature($target45);20$stored = $thermostatThermostat->temperature();
  4. public function temperature(): int

    11public function temperature(): int12{13    return $this->temperature50;14}
  5. $stored ← 50

    19$thermostat->setTemperature($target);20$stored→ 50 = $thermostatThermostat->temperature();2122echo "target=" . $target45 . "\n";23echo "stored=" . $stored50 . "\n";
    outputtarget=45
    stored=50
  1. $target ← 95, $thermostat ← Thermostat

    17$target→ 95 = 95;18$thermostat→ Thermostat = new Thermostat();19$thermostatThermostat->setTemperature($target95);20$stored = $thermostat->temperature();
  2. $this->temperature ← 90

    6public function setTemperature(int $temperature95): void7{8    $this->temperature→ 90 = max(50, min(90, $temperature95));9}
    values this stepThermostat$this
  3. $thermostat->setTemperature($target);

    18$thermostat = new Thermostat();19$thermostatThermostat->setTemperature($target95);20$stored = $thermostatThermostat->temperature();
  4. public function temperature(): int

    11public function temperature(): int12{13    return $this->temperature90;14}
  5. $stored ← 90

    19$thermostat->setTemperature($target);20$stored→ 90 = $thermostatThermostat->temperature();2122echo "target=" . $target95 . "\n";23echo "stored=" . $stored90 . "\n";
    outputtarget=95
    stored=90