Skip to content

Instantly share code, notes, and snippets.

@FlorianWolters
Last active December 14, 2015 15:29
Show Gist options
  • Save FlorianWolters/5108452 to your computer and use it in GitHub Desktop.
Save FlorianWolters/5108452 to your computer and use it in GitHub Desktop.
The class NotSoImmutableClass demonstrates how-to implement the Immutable Object pattern incorrectly.
<?php
namespace FlorianWolters;
/**
* The class {@see NotSoImmutableClass} demonstrates how-to implement the
* *Immutable Object* implementation pattern **incorrectly**
*
* @author Florian Wolters <[email protected]>
* @copyright 2013 Florian Wolters
* @license http://gnu.org/licenses/lgpl.txt LGPL-3.0+
* @link http://gist.github.com/FlorianWolters/5108452
*/
final class NotSoImmutableClass // The class is final and commented. Looks good!
{
/**
* The datetime of this object.
*
* @var DateTime
*/
private $dateTime;
/**
* Constructs a new object.
*
* @param DateTime $dateTime The datetime.
*/
public function __construct(DateTime $dateTime)
{
// TODO Should be:
// $this->dateTime = clone $dateTime;
$this->dateTime = $dateTime;
}
/**
* Returns the datetime of this object.
*
* @return DateTime The datetime.
*/
public function getDateTime()
{
// TODO Should be:
// return clone $this->dateTime;
return $this->dateTime;
}
}
// Lets check if the class is immutable.
// Lets start by testing the ingoing value of the class...
$constructorArgument = new DateTime('1985-09-28'); // We create the argument for the constructor.
$obj = new NotSoImmutableClass($constructorArgument); // We create a new instance of the class.
echo $obj->getDateTime()->format('Y-m-d'), \PHP_EOL; // 1985-09-28 - OK!
$constructorArgument->setDate(2001, 9, 11); // We modify the argument object.
echo $obj->getDateTime()->format('Y-m-d'), \PHP_EOL; // '2001-09-11'-> First fail!
// Uhh, lets try it with the outgoing value of the class...
$constructorArgument = new DateTime('1985-09-28');
$obj = new NotSoImmutableClass($constructorArgument);
$getterReturnValue = $obj->getDateTime(); // We retrieve the return object via the accessor method.
echo $getterReturnValue->format('Y-m-d'), \PHP_EOL; // '1985-09-28' -> OK!
$getterReturnValue->setDate(2013, 3, 7); // We modify the return object.
echo $obj->getDateTime()->format('Y-m-d'), \PHP_EOL; // '2003-03-07' -> Second fail!
// Uhh again. Looks like that class isn't THAT immutable...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment