개발자 Q&A

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

2025.04.28 02:04

ReflectionProperty::getDeclaringClass 관련 질문

목록
  • 백엔드버서커 16시간 전 2025.04.28 02:04 새글
  • 3
    1
제가 ReflectionProperty 클래스를 사용하여 클래스의 속성을 확인하고자 하는데, getDeclaringClass 메소드에 대해 혼동이 있습니다. 이 메소드는 어떤 클래스를 반환하나요?

예를 들어, 다음 코드는 어떤 클래스를 반환할까요?

php

class Person {

    public $name;



    function __construct($name) {

        $this->name = $name;

    }

}



class Student extends Person {

    public $grade;



    function __construct($name, $grade) {

        parent::__construct($name);

        $this->grade = $grade;

    }

}



$student = new Student('John Doe', 'A');

$reflectionClass = new ReflectionClass($student);

$reflectionProperty = $reflectionClass->getProperty('name');

$declaringClass = $reflectionProperty->getDeclaringClass();



echo $declaringClass->getName(); // 이 코드는 어떤 결과를 출력할까요?



getDeclaringClass 메소드는 어떤 클래스를 반환하는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  16시간 전



    getDeclaringClass 메소드는 속성을 선언한 클래스를 반환합니다.

    위 예제의 경우, $reflectionProperty->getDeclaringClass()은 Person 클래스를 반환합니다.

    이유는 Student 클래스가 Person 클래스를 상속하고, 속성 $name은 Person 클래스에 선언된 속성이기 때문입니다.

    따라서, echo $declaringClass->getName();은 "Person"을 출력합니다.

    2025-04-28 02:05

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

검색

게시물 검색