Skip to content

Instantly share code, notes, and snippets.

@twslade
Last active November 17, 2016 21:19
Show Gist options
  • Save twslade/bf61e6f37acaf6d9d0d15b4d9a7a55c2 to your computer and use it in GitHub Desktop.
Save twslade/bf61e6f37acaf6d9d0d15b4d9a7a55c2 to your computer and use it in GitHub Desktop.
<?php
$arr = ['one', 'two', 'three', 'four'];
//First class functions
//Functions in variables
$filter = function($item){
return strlen($item) > 3;
};
var_dump(array_filter($arr, $filter));
$val = 12;
$test = function($multiplier) use ($val) {
return $multiplier * $val;
};
var_dump($test(11));
$test = function(){return 'twinkle';};
var_dump($test('test'));
//Magic method invoke is used when class is called like function.
//This is what happens behind the scenes
class Twinkle{
protected $_star = 'star';
public function __invoke(){
return $this->_star;
}
}
$twinkle = new Twinkle();
var_dump($twinkle());
//Injecting functionality on the fly
// - Pros: Reduces classes/files | Future proof
// - Cons: Added complexity
class Comparer{
public $call;
public function comparere($x, $y, $call){
return $call($x, $y);
}
}
$comp = new Comparer();
//Largest number injection strategy
$intCompareRet = $comp->comparere(5, 1, function($x, $y){
return max($x, $y);
});
var_dump($intCompareRet);
//Largest string injection strategy
$stringCompareRet = $comp->comparere('test', 'testtest', function($x, $y){
return (max(strlen($x), strlen($y)) == strlen($x)) ? $x : $y;
});
var_dump($stringCompareRet);
//Lazy evaluation of variables since initial definition
$value = 12;
$getValue = function() use ($value){
return $value;
};
$value = 123;
var_dump($getValue());
//Equal to
class GetValue{
public function __construct($value){
$this->value = $value;
}
public function __invoke(){
return $this->value;
}
}
$value = 12;
var_dump(new GetValue($value));
$value = 13;
//Dependency Injection Example
class Container{
protected $keys = array();
public function __get($key){
return $this->keys[$key]($this);
}
public function __set($key, $func){
$this->keys[$key] = $func;
}
}
$test = new Container();
//Injecting dependencies without them existing!
$test->giveme5 = function($containerClassRef){
return 'The number passed is ' + $containerClassRef->num;
};
//
$test->num = function(){return 5;};
var_dump($test->giveme5);
//Dynamic field Mapping
class Importer{
public function __construct($mapper){$this->mapper = $mapper;}
public function mapData($source){
$ret = [];
foreach($this->mapper->mapping() as $field => $callback){
$ret[] = $callback($source);
}
return $ret;
}
}
class MyMapper{
public function mapping(){
$map= [];
$map['title'] = function($source){
return 'My title is ' . $source['title'];
};
$map['body'] = function($source){
return 'And my body !!content!! is ' . $source['content'];
};
$map['extra'] = function(){
return 'This is always hardcoded';
};
return $map;
}
}
$mapper = new MyMapper();
$importer = new Importer($mapper);
$source = ['title' => 'cool title', 'content' => 'cool body'];
var_dump($importer->mapData($source));
/**
*
*
* Memoization
*
*
*/
class TestMemoize{
public function memoize($func){
return function() use ($func){
static $results = array();
$args = func_get_args();
$key = serialize($args);
if(empty($results[$key])){
$results[$key] = call_user_func_array($func, $args);
return $results[$key];
}
return $results[$key] . ' memoized';
};
}
}
$test = new TestMemoize();
$while = function($i){
$return = '';
$iterator = 0;
while($iterator < $i){
$return .= $iterator . ', ';
$iterator++;
}
return $return;
};
var_dump($while(20));
$newWhile = $test->memoize($while);
$now = microtime(true);
var_dump($newWhile(5000000));
$then = microtime(true);
var_dump($then - $now);
$now = microtime(true);
var_dump($newWhile(5000000));
$then = microtime(true);
var_dump($then - $now);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment