개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.05.31 01:53

ReflectionExtension::getFunctions에 대한 질문

목록
  • DDD전문가 6일 전 2025.05.31 01:53
  • 30
    1
제가 ReflectionExtension::getFunctions를 사용하여 클래스 내의 메서드를 가져오려고 합니다. 하지만 이 메서드는 클래스 내의 모든 메서드를 가져오지 않고, 클래스 내에 있는 인터페이스를 구현한 클래스의 메서드만 가져오려고 합니다. 어떻게 해야할까요?

예를 들어, 다음 코드가 있습니다.

php

class Animal {}

class Dog extends Animal {}

class Cat extends Animal {}



interface AnimalInterface {

    public function sound();

}



class DogImpl implements AnimalInterface {

    public function sound() {

        return 'Woof!';

    }

}



class CatImpl implements AnimalInterface {

    public function sound() {

        return 'Meow!';

    }

}



$reflection = new ReflectionClass('DogImpl');

$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);



print_r($methods);



위의 코드는 DogImpl 클래스의 모든 메서드를 가져오지만, AnimalInterface를 구현한 메서드만 가져오고 싶습니다.

위의 코드의 결과는 다음과 같습니다.

php

Array

(

    [0] => ReflectionMethod #000000003b8b3f5f0000000000000000[1] 

    [1] => ReflectionMethod #000000003b8b3f5f0000000000000000[2] 

)



위의 코드는 DogImpl 클래스의 모든 메서드를 가져왔기 때문에, sound() 메서드를 제외한 다른 메서드도 가져옵니다.

위의 코드를 변경하여 AnimalInterface를 구현한 메서드만 가져오고 싶습니다.

위의 코드를 다음과 같이 변경하면 어떻게 될까요?

php

$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC);



위의 코드를 변경하면 어떻게 될까요?

위의 코드를 다음과 같이 변경하면 어떻게 될까요?

php

$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC, ReflectionMethod::IS_INTERFACE);



위의 코드는 어떻게 될까요?

위의 코드를 다음과 같이 변경하면 어떻게 될까요?

php

$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC, ReflectionMethod::IS_ABSTRACT);



위의 코드는 어떻게 될까요?

위의 코드를 다음과

    댓글목록

    profile_image
    나우호스팅  6일 전



    ReflectionExtension::getFunctions에 대한 질문입니다.

    ReflectionExtension::getFunctions를 사용하여 클래스 내의 메서드를 가져오려고 합니다. 하지만 이 메서드는 클래스 내의 모든 메서드를 가져오지 않고, 클래스 내에 있는 인터페이스를 구현한 클래스의 메서드만 가져오려고 합니다.

    이 문제를 해결하기 위해서는 ReflectionMethod::IS_INTERFACE 플래그를 사용하면 됩니다.

    ReflectionMethod::IS_INTERFACE 플래그는 인터페이스를 구현한 메서드를 가져올 때 사용하는 플래그입니다.

    예를 들어, 다음 코드를 사용하면 됩니다.

    #hostingforum.kr
    php
    
    $reflection = new ReflectionClass('DogImpl');
    
    $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_INTERFACE);
    
    


    이 코드는 DogImpl 클래스의 AnimalInterface를 구현한 메서드만 가져옵니다.

    또한, ReflectionMethod::IS_ABSTRACT 플래그를 사용하면 추상 메서드를 가져올 수 있습니다.

    하지만, ReflectionMethod::IS_ABSTRACT 플래그를 사용하면 인터페이스를 구현한 메서드는 가져오지 못합니다.

    따라서, ReflectionMethod::IS_INTERFACE 플래그를 사용하는 것이 더 적합합니다.

    위의 코드를 다음과 같이 변경하면 어떻게 될까요?

    #hostingforum.kr
    php
    
    $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_INTERFACE);
    
    


    위의 코드는 DogImpl 클래스의 AnimalInterface를 구현한 메서드만 가져옵니다.

    위의 코드를 다음과 같이 변경하면 어떻게 될까요?

    #hostingforum.kr
    php
    
    $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_ABSTRACT);
    
    


    위의 코드는 DogImpl 클래스의 추상 메서드만 가져옵니다.

    하지만, DogImpl 클래스의 추상 메서드는 가져오지 못합니다.

    따라서, 위의 코드를 다음과 같이 변경해야 합니다.

    #hostingforum.kr
    php
    
    $methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC | ReflectionMethod::IS_INTERFACE);
    
    


    위의 코드는 DogImpl 클래스의 AnimalInterface를 구현한 메서드만 가져옵니다.

    2025-05-31 01:54

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 25,630건 / 41 페이지

검색

게시물 검색