Skip to content

Instantly share code, notes, and snippets.

@suin
Created November 20, 2011 07:48
Show Gist options
  • Save suin/1379945 to your computer and use it in GitHub Desktop.
Save suin/1379945 to your computer and use it in GitHub Desktop.
自分のクラスで定義しているメソッドのみを返す: ReflectionClassを拡張して ref: http://qiita.com/items/1107
<?php
class Foo
{
public function aaa()
{
}
}
class Bar extends Foo
{
public function bbb()
{
}
}
class Baz extends Bar
{
public function ccc()
{
}
}
class MyReflectionClass extends ReflectionClass
{
/**
* Gets a list of methods which are defined in the self class.
* @param integer $filter
* @return array
*/
public function getMyMethods()
{
$myName = $this->getName();
$methods = call_user_func_array(array($this, 'getMethods'), func_get_args());
foreach ( $methods as $key => $method )
{
$className = $method->getDeclaringClass()->getName();
if ( $myName != $className )
{
unset($methods[$key]);
}
}
return $methods;
}
}
$fooClass = new MyReflectionClass('Foo');
var_dump($fooClass->getMyMethods());
/*
array(1) {
[0]=>
object(ReflectionMethod)#2 (2) {
["name"]=>
string(3) "aaa"
["class"]=>
string(3) "Foo"
}
}
*/
$barClass = new MyReflectionClass('Bar');
var_dump($barClass->getMyMethods());
/*
array(1) {
[0]=>
object(ReflectionMethod)#3 (2) {
["name"]=>
string(3) "bbb"
["class"]=>
string(3) "Bar"
}
}
*/
$bazClass = new MyReflectionClass('Baz');
var_dump($bazClass->getMyMethods());
/*
array(1) {
[0]=>
object(ReflectionMethod)#4 (2) {
["name"]=>
string(3) "ccc"
["class"]=>
string(3) "Baz"
}
}
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment