Hi
Am 2025-11-19 23:19, schrieb Rowan Tommins [IMSoP]:
On 18/11/2025 17:23, Larry Garfield wrote:
One thing I definitely do not like is the need for a `FileWrapper`
class in the RAII file-handle example. That seems like an unnecessary
level of abstraction just to squeeze the `fclose()` value onto the
file handle. The fully-separated Context Manager seems a more
flexible approach.
Yes, exploring how exactly that flexibility could be used was part of
my motivation for the examples I picked.
The downside is that it is slightly harder to understand at first
glance: someone reading "using (file_for_write('file.txt') as $fh)"
might well assume that $fh is the value returned from
"file_for_write('file.txt')", rather than the value returned from
"file_for_write('file.txt')->enterContext()".
What made sense to me was comparing to an Iterator that only goes
around once - in "foreach (files_to_write_to() as $fh)", the
"files_to_write_to()" call doesn't return $fh either,
"files_to_write_to()->current()" does.
For me the relevant keyword that indicates that the value is not used
directly is not the 'as', but the 'each' part of the 'foreach'. Just by
reading it as a English sentence, it becomes clear to me what is
happening.
The same is not true for me for `using (file_for_write('file.txt') as
$fh)` or even worse `using (new Manager() as $notActuallyTheManager)`
(which is part of the RFC). AFAICT the latter is not so much a problem
in Python, because there is no difference between constructors and
factory functions and also because there is no actual type declaration.
This means `open()` could be a function returning a file handle or it
could be the “constructor“ for a context manager (that then returns the
file handle as part of entering the context) and the difference is
effectively indistinguishable from the outside, which is not the case in
PHP.
Best regards
Tim Düsterhus