Created
March 28, 2012 12:54
-
-
Save JCook21/2225895 to your computer and use it in GitHub Desktop.
Recursive closure in PHP 5.4
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 | |
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