개발자 Q&A

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

2025.06.19 01:44

ReflectionProperty::__construct에 대한 도움을 요청합니다.

목록
  • 프론트마법사 오래 전 2025.06.19 01:44
  • 52
    1
제가 ReflectionProperty::__construct를 사용할 때, 다음과 같은 코드를 작성하였는데, 잘못된 부분이 있습니다.

php

$reflectionClass = new ReflectionClass('MyClass');

$property = $reflectionClass->getProperty('myProperty');

$property->__construct();



위의 코드에서, `ReflectionProperty::__construct`를 사용하여 `myProperty` 속성을 생성하려고 하지만, 에러가 발생합니다.

`ReflectionProperty::__construct`는 사용할 수 없다는 에러 메시지가 나옵니다.

`ReflectionProperty::__construct`를 사용할 수 없는 이유를 알려주시면 감사하겠습니다.

또한, `ReflectionProperty`를 사용하여 속성을 생성하는 방법을 알려주시면 감사하겠습니다.

감사합니다.

    댓글목록

    profile_image
    나우호스팅  오래 전



    `ReflectionProperty::__construct`는 사용할 수 없습니다. 이는 `ReflectionProperty`는 PHP의 내장 클래스로, PHP의 내장 클래스는 생성자를 호출할 수 없습니다.

    `ReflectionProperty`를 사용하여 속성을 생성하는 방법은 없습니다. `ReflectionProperty`는 이미 존재하는 속성에 대한 정보를 제공하는 클래스입니다. 속성을 생성하려면 PHP의 내장 클래스인 `ReflectionClass`를 사용하여 클래스를 생성한 후, 클래스 내에서 속성을 선언해야 합니다.

    예를 들어, `MyClass` 클래스를 생성한 후, `myProperty` 속성을 선언할 수 있습니다.

    #hostingforum.kr
    php
    
    class MyClass {
    
        public $myProperty;
    
    }
    
    
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $property = $reflectionClass->getProperty('myProperty');
    
    $property->setAccessible(true);
    
    $myObject = new MyClass();
    
    $myObject->$myProperty = '값';
    
    


    위의 코드에서, `ReflectionProperty`를 사용하여 `myProperty` 속성을 선언하고, 접근할 수 있도록 합니다.

    2025-06-19 01:45

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

검색

게시물 검색