-
-
Notifications
You must be signed in to change notification settings - Fork 18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Supported exporting closures with use() variables #7
Conversation
Hi, this feature looks great, thank you! A few questions:
|
throw new ExportException("The closure has bound variables through 'use', this is not supported.", $path); | ||
} | ||
|
||
$static = $reflectionFunction->getStaticVariables(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I didn't know that getStaticVariables()
returned use
statements, too. Is this documented anywhere? The manual doesn't say anything about it, so this makes me a bit chilly to rely on it!
This also made me curious: what happens if you have both a use
statement and a static
variable with the same name? It looks like use
wins, so at least we should be safe here:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
getStaticVariables()
returns op_array.static_variables
. This is intended for variables bound with static
, but closures also use this array for the use
variables.
I asked @nikic; This is an implementation detail and might be changed in the future (PHP 8). However, it should always be possible to get these vars using reflection. So if it's changed, a method like ReflectionFunction::getUseVariables()
would be added.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, with good unit tests we should be covered anyway. Maybe it's time to start testing against nightly
(with allow_failures
), so that we know right away if this implementation detail changes?
I don't think we should do anything special with referenced use variables like $foo = "Foo";
$var = ['x' => &$foo, 'y' => &$foo];
VarExporter::export($var); [
'x' => 'Foo',
'y' => 'Foo'
] Naming the option |
Makes sense, I guess. I'm never really comfortable with references. |
Added `CLOSURE_SNAPSHOT_USE` option.
Awesome work, thank you! 👍 Let's move on to #8. |
Released in 0.3.1 🚀 |
Added the
CLOSURE_USE_AS_VAR
CLOSURE_SNAPSHOT_USE
option to allow exporting closures withuse()
variables. Reflection is used to get the (current) value of these variables. These are exported by the exporter, parsed and inserted as statements.Example
Added
ClosureExporter::getParser()
method, not to create multiple parsers for exporting a single closure.