Skip to content

Instantly share code, notes, and snippets.

@JCook21
Created March 28, 2012 12:54
Show Gist options
  • Save JCook21/2225895 to your computer and use it in GitHub Desktop.
Save JCook21/2225895 to your computer and use it in GitHub Desktop.
Recursive closure in PHP 5.4
<?php
class foo {
public $bar;
public function __construct(Closure $closure)
{
$this->bar = Closure::bind($closure, $this);
}
public function __call($method, $args)
{
if (is_callable(array($this, $method)))
return call_user_func_array($this->$method, $args);
}
}
$foo = new foo(function($value){
//Use stripslashes here as an example but could be any function/operation.
return (is_array($value) ? array_map($this->bar, $value) : stripslashes($value));
});
$values = [
'\foo',
'\bar',
['O\'Reilly', '\\ABC']
];
$processed = $foo->bar($values);
var_dump($processed);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment