On 06/01/2020 21:01, Mike Schinkel wrote:
On Jan 6, 2020, at 5:56 AM, Robert Hickman <robehick...@gmail.com> wrote:
Would it be worth expanding the ideas of programmatic constant
definition into a more general compile-time code execution approach?
That would certainly be interesting. I do not know enough what is possible
there to opine.
It it were possible that might be the best of all worlds.
Anyone with experience in that area able to comment?
Isn't that the same as pre-processors, which elicited this response when
I mentioned them before:
Oh God no! Pre-processors are definitely an anti-pattern, in my book.
I came to that conclusion sometime in the 80's and have yet to see
anything that changed my mind.
Perhaps the phrase "pre-processor" put you too much in mind of C / M4
style text macros? If so, that's not what I meant at all; what I meant
was some tool that evaluated some syntax during a "build" step,
producing valid PHP code.
That might be as simple as string substitution, but it might be special
code blocks running standard PHP, but run as a separate build step and
the results inline into the code. It could be as general as this:
namespace MACROS {
function getenv($var) {
return $_ENV[$var];
}
}
class API {
const URL=MACRO\getenv('API_URL');
}
Parse the code to an AST, allowing function calls with literal arguments
wherever a literal value would normally be allowed. Find all function
calls to something in the MACRO namespace, evaluate them, and substitute
the result into the AST. Then write the modified AST to a new PHP file,
and deploy that to your server.
Obviously easier said than done, but there are tools out there like
https://preprocess.io/ which do some quite powerful things with this
approach.
Regards,
--
Rowan Tommins (né Collins)
[IMSoP]
--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php