I have an array of strings and I want to define functions with names being these strings. Is there a way to do that in PHP?
$a = array("xxx", "yyy", "zzz");
How do I programmatically define xxx(), yyy(), and zzz()?
Many thanks
If you really had to you could declare the function within an eval block:
foreach ($a as $functionname)
eval('
        function '.$functionname.' () {
            print 123;
        }
');
But that incurs some extra parsing time speed penalty over just declaring the functions in a file.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With