Skip to content

Instantly share code, notes, and snippets.

@Dragomeat
Created December 20, 2018 19:36
Show Gist options
  • Save Dragomeat/015928082a8fdc16741ca285154b27c2 to your computer and use it in GitHub Desktop.
Save Dragomeat/015928082a8fdc16741ca285154b27c2 to your computer and use it in GitHub Desktop.
<?php
declare(strict_types=1);
class Calculator
{
private $result;
public function __construct(int $result = 0)
{
$this->result = $result;
}
public function __invoke(int $number): self
{
$this->result += $number;
return $this;
}
public function __toString(): string
{
return (string) $this->result;
}
}
$calculator = new Calculator();
echo $calculator; // 0
echo $calculator(2)(3)(15); // 20
echo $calculator(2)(5)(5); // 32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment