Sebastian Bergmann wrote:
 You assume wrong. My point is that you should not use getMethod() when
 you do not know whether or not the method exists.

Ok, let's look at the options then:

5.0 - getMethods() [sure, you could probably use clever tricks to make this shorter but the general clumsiness will remain]:

foreach ($reflectionClass->getMethods() as $m)
{
        if ($m['name'] == "foo")
        {
                $method = $m;
                break;
        }
}

5.1 - hasMethod(): You were saying before that we don't need this and that's what I was basing my asumption on ;-) If we DO have hasMethod() then it is IMHO still unnecessarily complicated:

if ($reflectionClass->hasMethod("foo"))
        $method = $reflectionClass->getMethod("foo");

Call me old-fashioned but I like the assignment-as-truth-value style where you combine the assignement and check into one statement. De gustribus non est disbutandum though :-)

 Calling getMethod() for a method that does not exist is an error and
 should be consistently treated as such by raising an exception.

I disagree. The same could be said for getenv(). It's not part of a so-called OO API but it's the same operation: Return a key's value. It's half-way down the Python road where accessing undefined array indices causes exceptions. I like my PHP like my coffee: Simple and sweet :-)


Let me look at the problem from another point of view: What would your error handling with exceptions look like?

If you do things like
$reflectionClass->getMethod("foo")->...
then you have to catch your exception somewhere. Otherwise there is no difference to returning null as the program aborts. Now if you catch it in the same function you could as easily check for null. If on the other hand you're NOT catching it there your application has to know how to react to an unknown method. This can quickly lead to encapsulation problems. My point being that exceptions are very hard to handle properly and people disagree on how to do it (as seen in this thread) so they should be used sparingly and seldom be forced onto people. That's all I'm saying :-)


Happy holidays,
- Chris

--
PHP Internals - PHP Runtime Development Mailing List
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to