개발자 Q&A

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

2025.06.28 23:20

ReflectionMethod::__construct 이해에 도움이 필요합니다.

목록
  • 트랜잭션장인 21일 전 2025.06.28 23:20
  • 31
    1



    댓글목록

    profile_image
    나우호스팅  21일 전



    ReflectionMethod::__construct는 PHP ReflectionClass의 하위 클래스인 ReflectionMethod의 생성자입니다.

    이 생성자는 ReflectionMethod 인스턴스를 초기화하는 데 사용됩니다.

    인자로 $class와 $name이 전달됩니다.

    $class는 ReflectionMethod가 반영할 클래스의 이름입니다.

    $name은 반영할 메서드의 이름입니다.

    예를 들어, 다음 코드는 ReflectionMethod::__construct를 사용하여 ReflectionMethod 인스턴스를 초기화하는 방법을 보여줍니다.

    #hostingforum.kr
    php
    
    $reflectionMethod = new ReflectionMethod('MyClass', 'myMethod');
    
    


    이 코드는 MyClass 클래스의 myMethod 메서드를 반영하는 ReflectionMethod 인스턴스를 초기화합니다.

    반영된 메서드의 정보를 얻기 위해, getDeclaringClass(), getName(), getModifiers(), getNumberOfParameters(), getParameters(), getReturnType(), isAbstract(), isFinal(), isPrivate(), isProtected(), isPublic(), isStatic() 등의 메서드를 사용할 수 있습니다.

    예를 들어, 다음 코드는 반영된 메서드의 이름과 반환 타입을 출력하는 방법을 보여줍니다.

    #hostingforum.kr
    php
    
    $reflectionMethod = new ReflectionMethod('MyClass', 'myMethod');
    
    echo $reflectionMethod->getName() . "n"; // myMethod
    
    echo $reflectionMethod->getReturnType() . "n"; // ?string
    
    


    이 코드는 반영된 메서드의 이름과 반환 타입을 출력합니다.

    ReflectionMethod::__construct는 PHP ReflectionClass의 하위 클래스인 ReflectionMethod의 생성자이므로, ReflectionClass::__construct와 유사한 역할을 합니다.

    그러나, ReflectionMethod::__construct는 반영할 메서드의 이름을 인자로 전달해야 하므로, ReflectionClass::__construct와는 다릅니다.

    예를 들어, 다음 코드는 ReflectionClass::__construct와 ReflectionMethod::__construct의 차이를 보여줍니다.

    #hostingforum.kr
    php
    
    $reflectionClass = new ReflectionClass('MyClass');
    
    $reflectionMethod = new ReflectionMethod('MyClass', 'myMethod');
    
    


    이 코드는 ReflectionClass::__construct와 ReflectionMethod::__construct의 차이를 보여줍니다.

    ReflectionClass::__construct는 반영할 클래스의 이름만 인자로 전달하면 됩니다.

    반면에, ReflectionMethod::__construct는 반영할 클래스의 이름과 반영할 메서드의 이름을 모두 인자로 전달해야 합니다.

    이러한 차이로 인해, ReflectionMethod::__construct는 반영할 메서드의 이름을 인자로 전달해야 하므로, ReflectionClass::__construct와는 다릅니다.

    2025-06-28 23:21

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

검색

게시물 검색