ReflectionClass::hasMethod()

In a recent discussion on the PHP internals list Andi asked why ReflectionClass::getMethod() throws an exception when the requested method doesn’t exist instead of returning NULL. During the discussion it came out that it brings advantages to throw an exception when doing things like $class->getMethod(‚foo‘)->isInternal(); or if you really like exceptions. In addition it brings consistency with ReflectionMethod’s constructor which throws an exception too when a method can not be found. But since exceptions aren’t a good instrument just to check wether a method does (not) exist the conclusion was to add a ReflectionClass::hasMethod() method which simply returns a bool. Since I’m currently learning how this PHP stuff works I wrote a patch which was committed by Marcus this morning.

Für neue Blogupdates anmelden:


Ein Gedanke zu “ReflectionClass::hasMethod()

  1. offtopic but related: Generally I am a big exception hater for PHP. The only useful area for exceptions to me is to get rid of deeply nested if structures only for the sake of error handling. Something where a goto (only allowed to jump within the current scope) is probably cleaner.

    Why?

    The key to the PHP way is to be able to quickly wip up an application or more specifically a prototype. This is imparative to be able to maintain the fast way you can create highly useable finished code for your clients. The java world comes with all these modelling tools that let you design your apps in UML. So they have the 80% design rule and they are happy. The actualy coding ends up being 20% where you are just concerned with the last details. Exceptions are great there. Exceptions make sure you know what your code is doing always. They are great for writing bug free code.

    However modeling sucks. It fails to solve the real problem in programming. The real programming is not defining the interfaces of the insides of your application. The real programming challenge is to get the interface to the user just right.

    There its key to get that prototype up and running fast so that the users can start to play with things. UML is too abstract for them.

    Ok it seems I have gone off on a tangent. So wy are exceptions bad? Because they force you to write finished code. Finished bug free code. But that means you cant quickly make some modifications to your application to help your users become something that feels a bit more like what they have in mind. They dont need bug free code for that. They need to be able to see and touch the prototype in regards to the specific thing you are currently trying to visualize for them. Even if that means that you have an error string in a dropdown instead of a list of all countries that should normally go there.

    To conclude: during prototyping exceptions give you 0% output until you have fixed your code. Without exceptions your users may however be able to see 60% of the output, which may very well be 100% of what they needed to see at the current stage of development.

    Exceptions in php is turning php into a very very bad java clone without all the nice modeling to make it somewhat worthwhile.

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.