<?php
interface Counter
{
/** @return self */
public function count();
/** @return int */
public function getCount();
}
class ImmutableCounter implements Counter
{
private $count;
public function __construct($count = 0)
{
$this->count = (int) $count;
}
public function count()
{
return new ImmutableCounter($this->count + 1);
}
public function getCount()
{
return $this->count;
}
}
$counter = new ImmutableCounter();
$counter = $counter->count()->count()->count();
echo $counter->getCount();