Reputation: 19551
The ability to have closures is pretty new, so I'm not sure if there is a "standard way" to do this yet but...
Is there a good way to include a function in a PHP configuration file? Right now, the project I'm working on uses JSON for the configuration files, but I can't see any way to include PHP functions in it.
I'd like to be able to do something like this (using JSON syntax)
conf file:
{
myFunc: function($arg) {
return $arg . ' is an argument';
}
}
application:
json_with_func_literal_decode(...);
It doesn't have to use JSON syntax, although that'd be nice. Speed is not really a concern as long as it's reasonable. I would like to avoid the use of eval()
, which is the obvious solution to this problem.
Upvotes: 0
Views: 188
Reputation: 1526
You could simply use a PHP file without the overhead of parsing JSON/YAML for configuration like so:
<?php
return array(
'foo' => 'bar',
'myFunc' => function()
{
// ...
}
);
And retrieve the configuration like so:
$config = include 'config.php';
Upvotes: 3