Skip to content

Instantly share code, notes, and snippets.

@Madawar
Last active August 29, 2015 14:17
Show Gist options
  • Save Madawar/4dcefff453ee1ec70dcd to your computer and use it in GitHub Desktop.
Save Madawar/4dcefff453ee1ec70dcd to your computer and use it in GitHub Desktop.
Introduction to Closures in Php. in start.php we bind a closure to a class instance. In firstclosure.php we pass a closure to a class method in callable.php we improve the firstclosure.php class and finally in laravelstyle we explore how to call methods in laravel style read the blog at http://codedcell.com
<?php
/**
* Created by PhpStorm.
* User: dwanyoike
* Date: 03/03/15
* Time: 09:29
*/
class Addition
{
private $valueA;
private $valueB;
private $valueC;
private $sum;
public function AddNumbers()
{
$this->sum = $this->valueA + $this->valueB + $this->valueC;
return $this->sum;
}
/**
* Checks if function argument is Closure and callback and runs the function
* @param $callback
* @return mixed
*/
private function runCallback($callback)
{
if ($callback instanceof Closure and is_callable($callback)) {
return $callback();
}
}
public function setValueA($valueA)
{
$valueA = $this->runCallback($valueA);
$this->valueA = $valueA;
}
public function setValueB($valueB)
{
$valueB = $this->runCallback($valueB);
$this->valueB = $valueB;
}
public function setValueC($valueC)
{
$valueC = $this->runCallback($valueC);
$this->valueC = $valueC;
}
}
//Get the sum of the area of three circles
$instanceCircle = new Addition();
$instanceCircle->setValueA(function () {
return pi() * 14;
});
$instanceCircle->setValueB(function () {
return pi() * 36;
});
$instanceCircle->setValueC(function () {
return pi() * 44;
});
var_dump($instanceCircle->AddNumbers());
<?php
/**
* Created by PhpStorm.
* User: dwanyoike
* Date: 03/03/15
* Time: 09:29
*/
class Addition
{
private $valueA;
private $valueB;
private $valueC;
private $sum;
public function AddNumbers()
{
$this->sum = $this->valueA + $this->valueB + $this->valueC;
return $this->sum;
}
public function setValueA($valueA)
{
$this->valueA = $valueA;
}
public function setValueB($valueB)
{
$this->valueB = $valueB;
}
public function setValueC($valueC)
{
$this->valueC = $valueC;
}
}
//Get the sum of the area of three circles
$instanceCircle = new Addition();
$circleArea = function($radius){
return pi()*$radius;
};
$instanceCircle->setValueA( $circleArea(14));
$instanceCircle->setValueB($circleArea(36));
$instanceCircle->setValueC($circleArea(44));
var_dump($instanceCircle->AddNumbers());
<?php
/**
* Created by PhpStorm.
* User: dwanyoike
* Date: 03/03/15
* Time: 09:29
*/
class Addition
{
private $valueA;
private $valueB;
private $valueC;
private $sum;
public function AddNumbers($callback = null)
{
if ($callback instanceof Closure) {
call_user_func($callback, $this);
}
$this->sum = $this->valueA + $this->valueB + $this->valueC;
return $this->sum;
}
public function setValueA($valueA)
{
$this->valueA = $valueA;
}
public function setValueB($valueB)
{
$this->valueB = $valueB;
}
public function setValueC($valueC)
{
$this->valueC = $valueC;
}
public function total()
{
return $this->sum;
}
}
//Normal Usage of classes
$instanceA = new Addition();
$instanceA->setvalueA(120);
$instanceA->setvalueB(110);
$instanceA->setvalueC(150);
var_dump($instanceA->AddNumbers());
//Closure usage of class
$instanceB = new Addition();
$val = $instanceB->AddNumbers(function ($x) {
$x->setvalueA(120);
$x->setvalueB(110);
$x->setvalueC(150);
});
var_dump($val);
<?php
/**
* Created by PhpStorm.
* User: dwanyoike
* Date: 03/03/15
* Time: 09:29
*/
class Addition
{
private $valueA;
private $valueB;
private $valueC;
private $sum;
public function AddNumbers()
{
$this->sum = $this->valueA + $this->valueB + $this->valueC;
return $this->sum;
}
public function setValueA($valueA)
{
$this->valueA = $valueA;
}
public function setValueB($valueB)
{
$this->valueB = $valueB;
}
public function setValueC($valueC)
{
$this->valueC = $valueC;
}
}
//Normal Usage of classes
$instanceA = new Addition();
$instanceA->setValueA(10);
$instanceA->setValueB(10);
//setValueC
$ourClosure = function ($valueC)
{
$this->valueC = $valueC;
};
//bind our closure to a method and a class
$closure = Closure::bind($ourClosure,$instanceA,'Addition');
$closure(24);
var_dump($instanceA->AddNumbers());
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment