On 31/07/2025 14:46, Christian Schneider wrote:
$fh = try fopen($filePath, 'w') ignore (FileLockedException);
First of all: I'm wary because partial error handling seems dangerous to mel do
I know all possible Exception types and which ones should abort and which ones
should continue?
That's kind of the point: it's for when you know how to handle some
specific cases, but want *anything else* to abort.
In this hypothetical example, the code is using an exclusive lock to
avoid two processes writing to the file; it wants to gracefully handle
the specific scenario of "some other process has the lock". If there's
some other error, like "invalid file path", that *should not* be suppressed.
But the current PHP I/O functions give no way to distinguish:
$fh = @fopen($filePath, 'w');
if ( $fh === false ) {
// probably something else had the lock; could also be an invalid
file path, or a catastrophic disk failure ¯\_(ツ)_/¯
}
I have to admit that I'm also a sceptic of converting every possible
info/warning/error to an Exception but that's a different topic
I didn't intend to imply that this would replace all Warnings. Think of
it more as replacing the "returns false on error" part of the fopen()
signature.
If we decide to add something like the above I would very much prefer the try
... ignore block to be an expression with value null on error, making the first
line obsolete.
Good point, I agree.
--
Rowan Tommins
[IMSoP]