Re: [RFC] Context Managers, v2
| From: | David Maye | Date: | Fri, 06 Nov 2026 09:40:52 +0000 |
| Subject: | Re: [RFC] Context Managers, v2 | ||
| References: | 1 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-133020@lists.php.net to get a copy of this message | ||
Hello,I agree that
as seems more coherent than => in PHP. The only time
I saw => in PHP was with key => value. And in JS it was with () =>
{} functions.I have another question. This mechanism of Context Manager, Is it the same as
the Ruby do blocks? Example with Vagrant Ruby configuration
file:``Vagrant.configure("2") do |config| config.vm.box =
"bento/ubuntu-24.04" config.vm.box_version =
"202510.26.0"end`When you set the example as:`using
(file_for_write('file.txt') => $fp) { foreach ($someThing as $value) {
fwrite($fp, serialize($value)); }};// At this point, we're guaranteed that $fp has closed,
whether there was an error or not.`It means that the block inside using is
aware of variables outside of it's own scope, right?I like the idea. I have to admit that at
first I was confused because I thought that the first example in the RFC post was exactly the same
as the second example (Error catching included).I have another question: according to PHP
documentation ("Freeing resources" https://www.php.net/manual/en/language.types.resource.php#:~:text=makes%20no%20sense.-,Freeing%20resources,-%C2%B6):"Thanks
to the reference-counting system being part of the Zend Engine, a resource with no more references
to it is detected automatically, and it is freed by the garbage collector. For this reason, it is
rarely necessary to free the memory manually."From what I understand by this text,
fclose() is optional (but recommended according to some Stack Overflow posts). In both
cases (using or not the using using), this acts exactly the same. The only difference
is that you declare the variable inside the using instead of the outside the try/catch block, right?
Because even inside the using function you would need to invoke the fclose()`
function, right?Apart from easing the expressiveness of the language, I'm curious of what would
be other benefits. In the post you express that it provides "a way to abstract out common
control-flow and variable-lifetime-management pattern". What are the ways this function may
bring such improvements? Are those improvement observed by language like Python not already
available in PHP by default? I read somewhere that PHP tends to be more memory efficient than Python
due to the processing of the many magic methods and other issues. If so, Is the context manager in
Python focused on improving the variable-lifetime-management due to Python internal issues, and are
these issues present in PHP?Nonetheless, I enjoyed reading this post. Reminds me of Ruby. I like the
idea💡. I just got curious 😃On Nov 6, 2026 9:00 AM, Lazare Inepologlou
<linepogl@gmail.com> wrote:On Thu, 5 Nov 2026 at 19:33, Larry Garfield
<larry@garfieldtech.com> wrote:On Thu, Nov 5, 2026, at 12:18 PM, Stephen Reay wrote:
>
> Hi Larry,
>
> TLDR: why => rather than as?
>
> As a userland developer I like what this is trying to achieve. I'm not
> knowledgeable enough about the internals to comment on the finer
> details or their ramifications - I just have a bike shedding
> suggestion/question which seemed glaring to me so I wonder if it's
> because of an ambiguous syntax or some other issue?
>
> The => symbol seems odd to me, given how it's used
> elsewhere
> (generally as an indicator of a key/value pair, whether in an array or
> match block) and how generators/iterables are used elsewhere (ie
> foreach as)
>
> Your example is:
>
> using (file_for_write('file.txt') => $fp) {
>
> Whereas to my eye this seems like a much more obvious choice:
>
> using (file_for_write('file.txt') as $fp) {
>
> Anyway, just a thought.
The v1 RFC originally used as, which is what Python uses as well. However, a lot of
people were concerned that it implied that the left side expression would be "used as" the
right side variable. While in some cases it might be, the typical case is that it's not.
The context variable is emitted by the context manager, but is not the same thing.
At the time, no one could really agree on a better keyword. But => was right there waiting to
be used, and already has an "evaluates to" or "yields" or "produces"
meaning, which is roughly accurate to what happens here.
I don't mind as, personally, but the consensus before seemed to not care for it.
--Larry Garfield
Hello Larry,
Using the => operator for this case looks inconsistent to me as well. In my
perception, the meaning of => in PHP is that of mapping as a function in the
mathematical sense: the domain mapped to the image. For example, we have keys mapped to values in
arrays or in yield generators or even in iterations with foreach.
Similarly, the usage of => in lambda functions is also consistent as we have a
mapping of input to output.In this new case, however, it looks like it is used more as an
assignment. In the statement foreach ($iterable as $key => $value), the assignment
operator is as, while => consistently carries the meaning of a map. So,
I would argue that using as for assigning is more consistent with the previous syntax
choices.
Lazare INEPOLOGLOUArchitecte Logiciel