Last active
October 18, 2016 08:17
-
-
Save saroarhossain57/d6af5e1dc2e4dfd9364ea3a8b8c696ef to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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