Skip to content

Instantly share code, notes, and snippets.

@hellofromtonya
Created April 21, 2015 08:49
Show Gist options
  • Save hellofromtonya/8c1c7a0cb64c8ef40faa to your computer and use it in GitHub Desktop.
Save hellofromtonya/8c1c7a0cb64c8ef40faa to your computer and use it in GitHub Desktop.
Demo Calculator to illustrate Closure Dependency Injection
<?php namespace wpdevsclub_demo;
use Closure;
class Calculator {
public function solve( Closure $expression, $args ) {
if ( is_callable( $expression ) ) {
return $expression( $args );
}
}
public function add( $number1, $number2 ) {
return $number1 + $number2;
}
public function square( $number ) {
return $number * $number;
}
public function multiply( $number1, $number2 ) {
return $number1 * $number2;
}
}
$expression = function( $number ) {
echo 'fired!';
return $number * $number;
};
$calculator = new Calculator();
var_dump( $calculator->solve( $expression, 5 ) ); //* output 25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment