1

Completely unclear how to determine whether a method has a prototype or not.

Example:

<?php
class MyClass
{
    public function foo()
    {
    }
}

$refl = new ReflectionClass('MyClass');

var_dump($refl->getMethod('foo')->getPrototype());

Output for PHP 5.1.2 - 5.5.5:

Fatal error: Uncaught exception 'ReflectionException' with message 'Method MyClass::foo does not have a prototype'...

I would like to implement something like bool ReflectionMethod::hasPrototype( void ) for my ReflectionMethod wrapper.

Any ideas?

1 Answer 1

2

getPrototype is documented to throw if a prototype does not exist, so it's straightforward to convert that to a boolean:

public function hasPrototype()
{
    try {
        $this->getPrototype();
        return true;
    }
    catch (\ReflectionException $e) {
        return false;
    }
}
2
  • @Phil: I agree. But future-proofing at the cost of a single byte sounds like a no-brainer to me.
    – Jon
    Commented Oct 29, 2013 at 22:52
  • Just realised you're doing some kind of ReflectionMethod extension which could be anywhere. Good idea in that case though I'd just import (use) it at the top :)
    – Phil
    Commented Oct 29, 2013 at 22:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.