개발자 Q&A

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

2025.06.01 18:19

ReflectionClass::getInterfaceNames() 메서드 이해에 어려움

목록
  • 버전컨트롤러 4일 전 2025.06.01 18:19
  • 9
    1
저는 ReflectionClass::getInterfaceNames() 메서드를 공부 중인데, 이 메서드의 반환값이 interface 이름들의 배열이 된다고 하는데, 어떻게 반환되는지 잘 이해가 안 가요.

이 메서드는 어떤 interface를 참조하고 있는 클래스를 찾는 것처럼 보입니다. 예를 들어, 다음 코드를 보겠습니다.

php

class Animal {}

class Dog extends Animal {}

class Cat extends Animal {}



class PetStore {

    use Animal;

}



$reflection = new ReflectionClass('PetStore');

$interfaces = $reflection->getInterfaceNames();



print_r($interfaces);



이 코드를 실행하면 `$interfaces` 변수는 어떤 값을 가지는지 궁금합니다.

위의 코드에서 PetStore 클래스는 Animal 클래스를 사용하고 있기 때문에, `$interfaces` 변수는 Animal 인터페이스 이름을 가질 것 같은데, 실제로 어떤 값을 가지는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  4일 전



    위의 코드를 실행하면 `$interfaces` 변수는 빈 배열이 됩니다.

    이유는 `use Animal;` 문법은 Animal 클래스를 PetStore 클래스에 포함시켜주는 것이 아니라, Animal 클래스를 PetStore 클래스의 부모 클래스로 설정하는 것입니다.

    따라서, PetStore 클래스는 Animal 클래스를 상속받는 것이고, Animal 클래스는 인터페이스이기 때문에 `$interfaces` 변수는 빈 배열이 됩니다.

    만약, Animal 클래스가 인터페이스이면 `$interfaces` 변수는 ['Animal']이 됩니다.

    예를 들어, Animal 클래스가 인터페이스인 경우:

    #hostingforum.kr
    php
    
    interface Animal {}
    
    class Dog implements Animal {}
    
    class Cat implements Animal {}
    
    
    
    class PetStore {
    
        use Animal;
    
    }
    
    
    
    $reflection = new ReflectionClass('PetStore');
    
    $interfaces = $reflection->getInterfaceNames();
    
    
    
    print_r($interfaces); // ['Animal']
    
    

    2025-06-01 18:20

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

검색

게시물 검색