2010-04-12 4 views

답변

1

매우 흥미로운 질문입니다.

여기서 예제를 읽으시기 바랍니다. http://dsl093-056-122.blt1.dsl.speakeasy.net/edu/oreilly/Oreilly_Web_Programming_bookshelf/webprog/php/ch06_05.htm 인트로 스펙 션에 대한 더 깊은 통찰력을 제공합니다. 사용되는 수단에 대한 참조는 여기 - http://www.php.net/manual/en/ref.classobj.php 여기


테스트 케이스 기능이다. 그 전에는 사용할 수 없었던 Reflection을 사용하기 때문에 PHP 5 이상에서만 작동합니다. 당신은 여기에서 반사에 대한 자세한 내용을보실 수 있습니다 - http://www.php.net/manual/en/class.reflectionclass.php

<?php 

echo '<pre>'; 

class A { 
    public $pub_a = 'public a'; 
    private $priv_a = 'private a'; 
} 

class B extends A { 
    public $pub_b = 'public b'; 
    private $priv_b = 'private b'; 
} 

$b = new B(); 

print_r(getChildrenProperties($b)); 

function getChildrenProperties($object) { 
    $reflection = new ReflectionClass(get_class($object)); 
    $properties = array(); 

    foreach ($reflection->getProperties() as $k=>$v) { 
     if ($v->class == get_class($object)) { 
      $properties[] = $v; 
     } 
    } 

    return $properties; 
} 
+0

조금 해킹했지만 코드가 작동했습니다. 파고 들어 주셔서 감사합니다! –

1

또한 시도하고 사용 PHP 반영 할 수 http://php.net/manual/en/book.reflection.php

난 당신이 @Ivo Sabev 대답을한다하여 무엇을 할 수 있는지 추측 :

$properties = get_class_vars(ChildClass); 
$bproperties = get_class_vars(ParentClass); 

그리고 이제 $ bproperties에없는 모든 $ 속성을 반복합니다.

+0

그래,하지만이 기능은 실제로 당신에게 사유 재산을주지 않는다. 나는 ReflectionClass를보고 있지 않으며 그 트릭을 수행 할 수있는 작은 함수를 작성할 것이다. –

관련 문제