Skip to content

Instantly share code, notes, and snippets.

@saroarhossain57
Last active October 18, 2016 08:17
Show Gist options
  • Save saroarhossain57/d6af5e1dc2e4dfd9364ea3a8b8c696ef to your computer and use it in GitHub Desktop.
Save saroarhossain57/d6af5e1dc2e4dfd9364ea3a8b8c696ef to your computer and use it in GitHub Desktop.
<?php
/*
php method chaining is a technic which we can use for make your program more small. for this you have to return all the chained method all the class for this you have to use return $this;
*/
class Calculator{
private $num1;
private $num2;
private $result;
public function Set($num1, $num2){
$this->num1 = $num1;
$this->num2 = $num2;
return $this;
}
public function Sum(){
$this->result = $this->num1 + $this->num2;
return $this;
}
public function Sub(){
$this->result = $this->num1 - $this->num2;
return $this;
}
public function Result(){
return $this->result;
}
}
?>
<h1>PHP Method Chaining Example</h1>
<?php
$cal = new Calculator();
echo 'The result is : ';
echo $cal->Set(20, 10)->Sum()->Result();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment