Example of Mutable class

<?php

class
Car
{

/**
*
@var string
*/
private $color;

/**
*
@param string $color
*
*
@return Car
*/
public function setColor(string $color): Car
{
$this->color = $color;
return $this;
}

/**
*
@return string
*/
public function getColor(): string
{
return $this->color;
}
}
$car = new Car();
$car->setColor('red');
$car->setColor('green');
$car->setColor('black');

Example of Immutable class

<?php

class
Car
{
/**
*
@var string
*/
private $color;

/**
* Car constructor.
*
*
@param string $color
*/
public function __construct(string $color)
{
$this->color = $color;
}

/**
*
@return string
*/
public function getColor(): string
{
return $this->color;
}
}

$car = new Car('red');

Where to use immutable classes/objects?

Thank you for your attention

Senior web developer, architect, cryptocurrencies trader, https://lytvynov-anton.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store