Skip to content

Instantly share code, notes, and snippets.

@dwihujianto
Last active November 14, 2015 13:38
Show Gist options
  • Save dwihujianto/8b0b3fedd7ccfc07185a to your computer and use it in GitHub Desktop.
Save dwihujianto/8b0b3fedd7ccfc07185a to your computer and use it in GitHub Desktop.
<?php
class Math
{
protected $result = 0;
public function plus($value)
{
$this->result += $value;
return $this;
}
public function minus($value)
{
$this->result -= $value;
return $this;
}
public function result()
{
return $this->result;
}
}
$cmobj = new Math();
/* Common */
$cmobj->plus(10);
$cmobj->minus(2);
echo $cmobj->result(); // 8
/* Using method chaining */
$mcobj = new Math();
echo $mcobj->plus(13)->minus(3)->result(); //10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment