[WIP] Support 'static const CONST_NAME = expr;' #10
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Currently, this is limited to the global scope.
This can be thought of as similar to C:
the static const can only be seen within the namespace block scope.
Differences from global constants:
A name can only be declared once per namespace block.
The name can only be used by the following statements in the namespace block.
This can support any expression define() would accept
(function calls, $variable, properties, etc.).
This is eagerly evaluated when the expression might be dynamic.
This is deliberate, and meant to make accidental infinite recursion
(and unexpected side effects of fetching a constant for the first time)
less likely.
This may throw in the future.
The value from the first successful declaration is used and permanently stored.
Similarities to global constants:
(references, objects, and reference cycles.)
The current implementation allows code such as this:
Which is shorthand for:
Unfinished work:
(Fatal error? Leave as is?)
define()
call fails.(Fatal error? Throw a regular Error?)
Fix eval - account for it being possible when generating unique constant names based on the current file.
Fix loading the same file path with different contents - if
require
is called twice, so the file name isn't unique (For files, a hash of the file contents would make more sense and work better with opcache)Benchmark static const vs static variable for repeated calls
Undo limitation to top-level statements (block scope is a TODO). Add a variant of DECLARE_CONST that ignores the constant already existing.
Look into how to allow opcache to optimize
static const DEBUG = false; if (DEBUG) {}
with dead code detection