Created
November 11, 2019 11:41
-
-
Save rgpublic/499071d10bfe067ffc271f60f75ec132 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 SerializableClosure { | |
private $args; | |
private $code; | |
function __construct($args,$code) { | |
$this->args=$args; | |
$this->code=$code; | |
} | |
public function __invoke(&$arg1=null,&$arg2=null,&$arg3=null,&$arg4=null,&$arg5=null,&$arg6=null,&$arg7=null,&$arg8=null,&$arg9=null) { | |
$argstr=implode(",",$this->args); | |
$paras=[]; | |
for ($k=1;$k<=count($this->args);$k++) $paras[]='$arg'.$k; | |
$paras=implode(",",$paras); | |
return eval('return (function ('.$argstr.') {'.$this->code.'})('.$paras.');'); | |
} | |
} | |
$closure=new SerializableClosure(['&$a','&$b'],'$a++;$b++;return $a+$b;'); | |
$a=7;$b=5; | |
echo $closure($a,$b)."\n"; | |
echo $a."/".$b."\n"; | |
echo "====\n"; | |
$serialize=serialize($closure); | |
$new_closure=unserialize($serialize); | |
$a=7;$b=5; | |
echo $new_closure($a,$b)."\n"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment