Created
April 21, 2015 08:49
-
-
Save hellofromtonya/8c1c7a0cb64c8ef40faa to your computer and use it in GitHub Desktop.
Demo Calculator to illustrate Closure Dependency Injection
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 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