PHP: Write anonymous function to file for later use

42 Views Asked by At

Let's assume, we have an array with anonymous functions as values. It has following contents:

  $fpool = [
    'f1' => function($a){
        if($a > 0){
          return [$a*$a, $a+$a];
        }
        return 0;
     },

    'f2' => function($a){
       if($a > 0){
         return [$a*3, $a+2];
       }
       return 1;
    }
];

I want to take some values and save them to separate file without copy/paste.

Is it possible for example to save $fpool['f2'] to separate file for later include?

The file to include must have followng content:

   <?php
       return ['f2' => function($a){....}]

Because its a closure I can't serialize() or do var_export()

0

There are 0 best solutions below