On lundi 13 juin 2022 15:36:26 CEST Rowan Tommins wrote:
> > Auto-capture in PHP is by-value. This makes this impossible. It also makes
> > explicit declarations non-necessary and much less useful.
> > 
> > Live-variable analysis is mentioned in as part of implementation details.
> > It should not be necessary to understand these details to understand the
> > behavior of auto-capture.
> 
> As noted in my other e-mail, by-value capture can still have side
> effects, so users may still want to ensure that their code is free of
> such side effects.

My choice of words in this reply was inaccurate when I said "In these 
languages it is easy to accidentally override/bind a variable from 
the parent scope by forgetting a variable declaration.", since "override" can 
be interpreted in different ways.

What I meant here is that it is not possible to accidentally bind a variable 
on the parent scope. This is actually impossible unless you explicitly capture 
a variable by-reference. Do you agree with this ?

Possible side-effects via object mutations are documented in the "No 
unintended side-effects" section of the RFC. This assumes that property 
assignments or method calls to captured objects would be intended, since these 
assignments/calls would result in an error if the variable was not defined and 
not captured. Do you have examples where assignments/calls would non-
intendedly cause a side effect, with code you would actually write ?

> As noted in my other e-mail, by-value capture can still have side 
> effects, so users may still want to ensure that their code is free of 
> such side effects.

There are two ways for a closure to have a side-effect (already documented in 
the RFC) :

- The closure explicit captures a variable by reference, and bind it
- The closure mutates a value accessed through a captured variable. Mutable 
values include objects and resources, but NOT scalars or arrays (since they 
are copy-on-write).

In the first case, this is entirely explicit.

In the second case, the only thing you need do understand is that if you 
access a variable you did not define, the variable is either undefined or 
comes from the declaring scope. Accessing undefined variables is an error, so 
it must come from the declaring scope.

Your example uses isset(), which is valid code in most circumstances, but as 
you said it's not particularly good code. Do you have other examples that come 
to mind ?

> Currently, the only way to do so is to understand the "implementation
> details"

I'm willing to make changes if that's true, because I definitely don't want 
this to be the case.

-- 
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: https://www.php.net/unsub.php

Reply via email to