Another PHP WTF


  • sockdevs

    So I'm working on some PHP that gets to create its own autoloader since the environment to which it is applied does not have one, and I want to throw an exception if a given file requested cannot be found.

    PHP internally only defines two base exceptions - Exception and ErrorException (the latter is if you have the standard error handler set up to throw everything as exceptions rather than conventional errors), and with 5.1 they introduced the Standard PHP Library, aka SPL which added a bunch of exceptions.

    This means we have a bunch of exceptions that we can treat as standard, even though internally they weren't as such. There's BadMethodCallException and BadFunctionCallException and RuntimeException

    But you know what there isn't?

    That's right. No FileNotFoundException. Fucking PHP is the worst of the worst of TRWTF.



  • ExceptionNotFoundException

    Is your name Simon?


  • sockdevs

    Hahahahahah



  • @Arantor said:

    But you know what there isn't?

    That's right. No FileNotFoundException. Fucking PHP is the worst of the worst of TRWTF.

    If it makes you feel any beter, the System namespace in .NET doesn't have one either...


    Filed under: System.IO - on the other hand...



  • Result of the weird mismash of C, Perl and C++/Java/C# programming language style that calls itself PHP.



  • It also doesn't have a UnicornException. So what? Do you really except the SPL to contain exceptions for all your possible specific problems? And then people bitch about "Ugh, look at all those useless exception classes in PHP. Why did they put a DateIsInThePastException in the PHP core?!"

    Throw a RuntimeException and just inherit your own one from that one and be done. This also gives you the wonderful opportunity to namespace your exception so people can have better catch blocks.

    At least pick a worthy battle when bitching about PHP.



  • @Arantor said:

    That's right. No FileNotFoundException. Fucking PHP is the worst of the worst of TRWTF.

    Python only got FileNotFoundError in 2012. Before that you only had IOError, which was raised for almost any OS related error (except in some cases, which was pretty confusing).
    Maybe PHP will do the same thing in a few years, who knows?


  • sockdevs

    No, but here's the thing about SPL - it has a bunch of exceptions for situations less common than a file not being found being part of the standard library.

    Aside from BadFunctionCallException, BadMethodCallException, there's DomainException, InvalidArgumentException and plenty more. It doesn't have anything relating to I/O as standard exceptions (as @VinDuv was observing about Python)

    As for extending RuntimeException, that's almost hilarious. Consider the actual use case I outlined. I'm building an autoloader. It is theoretically possible that the class containing whatever is extending RuntimeException itself won't be available.

    IOW for this use case, I really wanted something built in rather than having to load something that might not be, but I've just gone with RuntimeException since due to the age of the codebase I'm grafting this onto, it's not like anything else is throwing exceptions to worry about being caught...



  • @RandyHickey said:

    Do you really except the SPL to contain exceptions for all your possible specific problems?

    Does SPL do file IO? (Asking, 'coz I don't know.) If so, it can reasonably be expected to provide exception classes for common IO errors. If not, then whatever library contains such functionality should. I am assuming there is one...


  • sockdevs

    Answer: sort of. There are some classes in SPL to do things like recursing through a directory structure conveniently, as well as a class outright called SplFileInfo.

    The docs on its constructor assert that the file does not need to exist, but if it doesn't, it's not like you can use that class to write to it or anything.

    And this is where it gets hilarious. Observe the following sample:

    <?php
    $file = new SplFileInfo('./doesnotexist.php');
    echo $file->getMTime();
    ?>
    

    This does two things. Firstly, it throws a regular error to note that stat failed (consistent with other file functions in PHP, throwing errors not exceptions). Then secondly, and hilariously, it throws a RuntimeException to denote the same thing when even a generic IOException would be an improvement on this, though a FileNotFoundException would be even better.



  • @Arantor said:

    Firstly, it throws a regular error to note that stat failed (consistent with other file functions in PHP, throwing errors not exceptions). Then secondly, and hilariously, it throws a RuntimeException to denote the same thing when even a generic IOException would be an improvement on this, though a FileNotFoundException would be even better.

    So basically, it'll tell you that your program failed - in two different ways, in fact - but not why? Great!


  • sockdevs

    PHP is TRWTF, what can I say?

    This is why I think it should have a FileNotFoundException, or at the very least something I/O related. But some people here think that's not necessary... :smiling_imp:


Log in to reply
 

Looks like your connection to What the Daily WTF? was lost, please wait while we try to reconnect.