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());
@Herzult
Copy link

Herzult commented Mar 1, 2013

PHP Version:

PHP 5.4.6-1ubuntu1.1 (cli) (built: Nov 15 2012 01:18:34) 
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

Result:

class ReflectionMethod#3 (2) {
  public $name =>
  string(9) "getKernel"
  public $class =>
  string(8) "BaseTest"
}
PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Method BaseTest::getKernel does not have a prototype' in /home/antoine/htdocs/plop/foo.php:26
Stack trace:
#0 /home/antoine/htdocs/plop/foo.php(26): ReflectionMethod->getPrototype()
#1 {main}
  thrown in /home/antoine/htdocs/plop/foo.php on line 26

Fatal error: Uncaught exception 'ReflectionException' with message 'Method BaseTest::getKernel does not have a prototype' in /home/antoine/htdocs/plop/foo.php on line 26

ReflectionException: Method BaseTest::getKernel does not have a prototype in /home/antoine/htdocs/plop/foo.php on line 26

Call Stack:
    0.0002     239304   1. {main}() /home/antoine/htdocs/plop/foo.php:0
    0.0003     241328   2. ReflectionMethod->getPrototype() /home/antoine/htdocs/plop/foo.php:26

@clemherreman
Copy link

PHP Version

clemherreman≻ macclem≻ ~ ≻ Boulot ≻ Perso ≻ $ ≻php -v
PHP 5.4.3 (cli) (built: Jun  6 2012 11:03:31)
Copyright (c) 1997-2012 The PHP Group
Zend Engine v2.4.0, Copyright (c) 1998-2012 Zend Technologies
    with Xdebug v2.2.1, Copyright (c) 2002-2012, by Derick Rethans

Result

 clemherreman≻ macclem≻ ~ ≻ Boulot ≻ Perso ≻ $ ≻php gistfile1.php
class ReflectionMethod#3 (2) {
  public $name =>
  string(9) "getKernel"
  public $class =>
  string(8) "BaseTest"
}
PHP Fatal error:  Uncaught exception 'ReflectionException' with message 'Method BaseTest::getKernel does not have a prototype' in /Users/clemherreman/Boulot/Perso/gistfile1.php:26
Stack trace:
#0 /Users/clemherreman/Boulot/Perso/gistfile1.php(26): ReflectionMethod->getPrototype()
#1 {main}
  thrown in /Users/clemherreman/Boulot/Perso/gistfile1.php on line 26

@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