Am 31.07.2025 um 17:53 schrieb Rowan Tommins [IMSoP] <imsop....@rwec.co.uk>:
> 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 ¯\_(ツ)_/¯
> }

If you have to handle null afterwards (e.g. avoiding writing to invalid $fh) 
then the "ignore" version does not really help much IMHO

Realistically I think it would be something like

if ($fh = try fopen($filePath, 'w') ignore (FileLockedException)) {
        ...
} [else
        ...]

vs. currently

try {
        $fh = try fopen($filePath, 'w');
        ...
} catch (FileLockedException) {
        [...]
}

which does not seem much clearer to me.

> 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.

... so you would replace false for (some) errors by Exceptions, that's what I 
meant. Plus having some stuff return false and some things throw Exceptions 
seems weird too, that's why I said "all" in the first place. But false/null vs. 
Exceptions is a different, much broader topic, let's skip that for now ;-)

Regards,
- Chris

Reply via email to