2011-12-24 2 views
2

다른 개체에 개체 인스턴스를 프로그래밍하고 있습니다. 이제 인스턴스화 된 객체의 유효성을 검사해야합니다.서브 클래스의 모든 메소드를 얻는 방법?

내가 사용하고있는 코드는 정확하지만 개체는 다른 개체의 자식이므로 부모의 뒷쪽 메서드가됩니다.

코드 :

<?php 
class MyParentClass 
{ 
    ... 

    $objectName = "subClassExample"; 
    $obj = new $objectName(); 
    print_r(get_class_methods($obj)); 

    ... 
} 
?> 

반환 :

Array ([0] => __construct [1] => myMethod) 

서브 클래스 :

<?php 
class subClassExample extends parentClass 
{ 

    public function myMethod() 
    { 
     return null; 
    } 
} 
?> 

나는 반환해야합니다

Array ([0] => myMethod) 

상위 클래스 : 내가 도울 수 있기를 바랍니다

<?php 
class parentClass 
{ 

    function __construct() 
    { 
     return null; 
    } 
} 
?> 

을, 내가 정말 감사드립니다. 인사말!

P .: Excuse My English is not my language. 나는 스페인어와 노르웨이어 Bokmal을 사용합니다.

+0

'$ objectName'이란 무엇입니까? 어떤 물건입니까? – FakeRainBrigand

+0

$ objectName은 Object name입니다 ... 예 : $ objectName = 'subClassExample'; –

답변

2

당신은 PHP's Reflection­Docs으로이 작업을 수행 할 수 있습니다

class Foo 
{ 
    function foo() {} 
} 

class Bar extends Foo 
{ 
    function bar() {} 
} 

function get_class_methodsA($class) 
{ 
    $rc = new ReflectionClass($class); 
    $rm = $rc->getMethods(ReflectionMethod::IS_PUBLIC); 

    $functions = array(); 
    foreach($rm as $f) 
     $f->class === $class && $functions[] = $f->name; 

    return $functions; 
} 

print_r(get_class_methodsA('Bar')); 

출력 : 만 UNIQUE 자녀의 방법을해야하는 경우 아이 안에이 수표 나 부모 클래스를 할 수

Array 
(
    [0] => bar 
) 
+0

우수, 감사합니다! 그것은 증명되었지만 문서와 함께 만 나와 나를 좋아하는 것처럼 보입니다 (: -)) –

0

:

$cm = get_class_methods($this); //Get all child methods 
$pm = get_class_methods(get_parent_class($this)); //Get all parent methods 
$ad = array_diff($cm, $pm); //Get the diff 

유의 사항 : get_class_methods r 모든 유형의 메소드 (public, protected 등)를 반환합니다.

관련 문제