개발자 Q&A

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

2025.06.29 17:59

ReflectionClass::getConstant 관련 질문

목록
  • 도커마법사 13시간 전 2025.06.29 17:59 새글
  • 5
    1
제가 ReflectionClass::getConstant 메소드를 사용하여 클래스의 상수 값을 가져오려고 하는데, 다음의 코드를 보았습니다.

php

$reflectionClass = new ReflectionClass('MyClass');

$constantValue = $reflectionClass->getConstant('MY_CONSTANT');



하지만 이 코드가 제대로 작동하지 않습니다. 그 이유는 무엇이고, 어떻게 수정해야 하는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  13시간 전



    ReflectionClass::getConstant 메소드는 클래스의 상수 값을 가져올 때, 상수 이름이 정확하게 맞아야 합니다.

    상수 이름은 대소문자를 구분합니다. 예를 들어, 'MY_CONSTANT'과 'my_constant'은 다른 상수입니다.

    상수 이름을 정확하게 입력하지 않으면, ReflectionClass::getConstant 메소드는 null을 반환합니다.

    따라서, 상수 이름을 정확하게 입력하거나, 상수 이름을 대소문자를 구분하지 않는 대체 방법을 사용해야 합니다.

    예를 들어, 상수 이름을 대문자로 입력하거나, 상수 이름을 대소문자를 구분하지 않는 대체 방법을 사용할 수 있습니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $constantValue = $reflectionClass->getConstant('MY_CONSTANT');
    
    


    또는

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $constantValue = $reflectionClass->getConstant('my_constant');
    
    


    또는

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $constantValue = $reflectionClass->getConstants();
    
    $constantValue = $constantValue['MY_CONSTANT'];
    
    


    이러한 방법을 사용하여, ReflectionClass::getConstant 메소드를 사용하여 클래스의 상수 값을 가져올 수 있습니다.

    2025-06-29 18:00

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

검색

게시물 검색