Skip to content

Instantly share code, notes, and snippets.

@docteurklein
Created March 1, 2013 13:53
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save docteurklein/5064805 to your computer and use it in GitHub Desktop.
Save docteurklein/5064805 to your computer and use it in GitHub Desktop.
failing test case for ReflectionMethod::getPrototype() when a class is using a trait that defines a method, chich class extends another class that defines the same method too.
<?php
trait TestTrait
{
public function getKernel()
{
}
}
class BaseTest
{
public function getKernel()
{
}
}
class Test extends BaseTest
{
use TestTrait;
}
$refl = new \ReflectionClass('Test');
var_dump($refl->getMethod('getKernel')->getPrototype());
var_dump($refl->getMethod('getKernel')->getPrototype()->getPrototype()->getPrototype()->getPrototype()->getPrototype()->getPrototype()->getPrototype()->getPrototype()->getPrototype()->getPrototype()->getPrototype());
@docteurklein
Copy link
Author

@sergeyklay
Copy link

PHP 5.5.5 (cli) (built: Oct 16 2013 05:59:03) 
Copyright (c) 1997-2013 The PHP Group
Zend Engine v2.5.0, Copyright (c) 1998-2013 Zend Technologies
    with Xdebug v2.2.3, Copyright (c) 2002-2013, by Derick Rethans
<?php

class MyMethod extends \ReflectionMethod {

    public function hasPrototype() {
        try {
            parent::getPrototype();
            return true;
        }
        catch (\ReflectionException $e) {
            return false;
        }
    }

    public function getPrototype() {
        return $this->hasPrototype() ? parent::getPrototype() : "Method {$this->getName()} does not have a prototype";
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment