개발자 Q&A

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

2025.04.11 18:43

ReflectionObject::__construct 이해하기

목록
  • Go매니아 3일 전 2025.04.11 18:43
  • 19
    1
제가 ReflectionObject를 사용하여 클래스의 정보를 얻어내는 방법을 공부하고 있지만, __construct 메서드에 대해서 잘 이해가 가지 않습니다.

ReflectionObject::__construct는 클래스를 생성할 때 자동으로 호출되는 메서드 인가요?

예를 들어, 다음 코드가 있다고 가정해 보겠습니다.

php

class TestClass {

    public function __construct($name) {

        echo "TestClass 생성";

    }

}



$reflection = new ReflectionClass('TestClass');

$reflection->newInstance('이름을 넣어');



위 코드에서 $reflection->newInstance()는 TestClass 클래스의 인스턴스를 생성할 때 자동으로 __construct 메서드가 호출되는 것 인가요?

혹시 다른 방법으로도 __construct 메서드를 호출할 수 있는 방법이 있을까요?

혹시 __construct 메서드에 대한 더 많은 정보가 필요하실까요?

    댓글목록

    profile_image
    나우호스팅  3일 전



    ReflectionObject::__construct는 클래스의 생성자 메서드를 반환하는 메서드입니다. 클래스를 생성할 때 자동으로 호출되는 메서드는 __construct가 아니고, new 키워드를 사용하여 클래스를 인스턴스화할 때 자동으로 호출됩니다.

    예를 들어, 다음 코드에서 TestClass 클래스의 인스턴스를 생성할 때 __construct 메서드가 호출됩니다.

    #hostingforum.kr
    php
    
    class TestClass {
    
        public function __construct($name) {
    
            echo "TestClass 생성";
    
        }
    
    }
    
    
    
    $reflection = new ReflectionClass('TestClass');
    
    $reflection->newInstance('이름을 넣어');
    
    


    위 코드에서 $reflection->newInstance()는 TestClass 클래스의 인스턴스를 생성할 때 자동으로 __construct 메서드가 호출되는 것이 아닙니다. __construct 메서드는 new 키워드를 사용하여 클래스를 인스턴스화할 때 자동으로 호출됩니다.

    #hostingforum.kr
    php
    
    $testClass = new TestClass('이름을 넣어');
    
    


    위 코드에서 TestClass 클래스의 인스턴스를 생성할 때 __construct 메서드가 호출됩니다.

    __construct 메서드를 호출하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    $reflection = new ReflectionClass('TestClass');
    
    $reflection->newInstance('이름을 넣어');
    
    


    또는

    #hostingforum.kr
    php
    
    $testClass = $reflection->newInstance('이름을 넣어');
    
    


    또는

    #hostingforum.kr
    php
    
    $reflection = new ReflectionClass('TestClass');
    
    $reflection->newInstanceArgs(array('이름을 넣어'));
    
    


    __construct 메서드에 대한 더 많은 정보는 PHP의 생성자 메서드에 대한 설명을 참고하시기 바랍니다.

    2025-04-11 18:44

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

검색

게시물 검색