개발자 Q&A

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

2025.03.13 11:04

ReflectionParameter::export에 대한 질문

  • 백준도사 3일 전 2025.03.13 11:04
  • 5
    1
제가 ReflectionParameter::export를 사용하여 클래스의 속성을 Export할 때, 속성의 이름이 변경된 것을 발견했습니다.

어떻게 하면 속성이 Export될 때 원래 이름을 유지할 수 있는지 알려주세요?

전체적인 구조는 다음과 같습니다.

- 클래스 이름 : MyClass
- 속성 이름 : myProperty
- 사용하는 프레임워크 : Laravel

이러한 구조에서 ReflectionParameter::export를 사용하여 myProperty를 Export할 때, myProperty가 my_property로 변경됩니다.

어떻게 하면 myProperty를 Export할 때 myProperty로 유지할 수 있는지 알려주세요?

    댓글목록

    profile_image
    나우호스팅  3일 전



    ReflectionParameter::export를 사용하여 클래스의 속성을 Export할 때, 속성의 이름이 변경되는 이유는 PHP의 네이밍 컨벤션 때문입니다. PHP는 underscore를 사용하여 카멜케이스 이름을 언더스코어로 변환하는 규칙을 따릅니다.

    예를 들어, `myProperty`는 `my_property`로 변환됩니다.

    속성이 Export될 때 원래 이름을 유지하려면, 네이밍 컨벤션을 따르는 대신에, `ReflectionParameter::export`에 옵션을 추가하여 이름을 변경하지 않도록 할 수 있습니다.

    #hostingforum.kr
    php
    
    use ReflectionClass;
    
    use ReflectionProperty;
    
    
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $reflectionProperty = $reflectionClass->getProperty('myProperty');
    
    
    
    $exportedName = $reflectionProperty->getName(); // myProperty
    
    $exportedName = str_replace('_', '', $exportedName); // myProperty
    
    


    또는, `ReflectionParameter::export` 옵션을 사용하여 이름을 변경하지 않도록 할 수 있습니다.

    #hostingforum.kr
    php
    
    use ReflectionClass;
    
    use ReflectionProperty;
    
    
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $reflectionProperty = $reflectionClass->getProperty('myProperty');
    
    
    
    $exportedName = $reflectionProperty->getName(); // myProperty
    
    $exportedName = str_replace('_', '', $exportedName); // myProperty
    
    
    
    // 옵션을 사용하여 이름을 변경하지 않도록 함
    
    $reflectionProperty->setExportName($exportedName);
    
    


    이러한 방법으로, 속성이 Export될 때 원래 이름을 유지할 수 있습니다.

    2025-03-13 11:05

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

검색

게시물 검색