0

I'll start with a minimal example. Suppose we have the following dependency in our project:

<?php
namespace VeryUsefulFilesystemLibrary;

function read(string $filepath): string|false
{
    return file_get_contents($filepath);
}

And our project looks like:

<?php
require 'vendor/autoload.php';

$filedata = VeryUsefulFilesystemLibrary\read('/path/to/file');

// do something depending on $filedata (signature: string|false)

At some point (perhaps early in development), we decided to use a custom error handler like filp/whoops:

<?php
require 'vendor/autoload.php';

(new Whoops\Run)
    ->pushHandler(new Whoops\Handler\PlainTextHandler)
    ->register();

$filedata = VeryUsefulFilesystemLibrary\read('/path/to/file');

// how about Whoops\Exception\ErrorException now? :)

Now all potential warnings that were previously logged but processed correctly, now killing the app. I could use try/catch, but, firstly, e.g. fopen() with try/catch looks extremely weird, and secondly, I have to think more. Much more. And… I can't even find the words, how… wrong is this?

Personally, I would prefer that absolutely any problem in PHP is an exception. Exactly how Whoops does to him. But I don't understand how to work in this way with a language that behaves differently by default, and therefore doesn't have the necessary documentation (the same applies to dependencies).

Am I missing something?

  • Use `@` to suppress the warnings in the places where you know that errors are OK? – Barmar Apr 06 '21 at 00:24
  • @Barmar, no. Firstly, I'll lose the reason that way. Even though I know how to handle e.g. `session_start() === false`, I should know why this didn't work. Secondly, I really can't know which functions might warn. It's very rare to see something about this in the docs. Sorry, I kind of contradict myself. I say that I want to use a custom error handler, but at the same time I don't seem to want to use it. I'm just trying to figure out how experienced guys do it. –  Apr 06 '21 at 00:43
  • You seem to be making an assumption that custom error handling is something that's commonly used. I doubt it. – Barmar Apr 06 '21 at 00:47
  • @Barmar, maybe, but if I'm not mistaken, this is included by default in some popular frameworks. And I can't believe that large projects don't use such things. Although at the moment I can't believe it either, because the default handler looks much more transparent to me, albeit less convenient. –  Apr 06 '21 at 01:00
  • While it's hard, I'm sure some programmers try hard to avoid any warnings. They check the inputs carefully. – Barmar Apr 06 '21 at 01:01
  • @Barmar, that's right, I do it too. But it's impossible to think through all the cases. And the question isn't about that :) –  Apr 06 '21 at 01:16
  • Then they live with the consequence that the application crashes if they missed something, and they fix them when they come up. – Barmar Apr 06 '21 at 01:18

0 Answers0