개발자 Q&A

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

2025.05.07 09:04

ReflectionAttribute::getName 관련 질문입니다.

목록
  • 화이트해커연구가 1일 전 2025.05.07 09:04
  • 1
    1
제가 ReflectionAttribute::getName을 사용하여 클래스의 속성을 가져오려고 하는데,
속성이 없을 때 예외가 발생하는 것을 막고 싶습니다.

속성이 없을 때는 어떤 값을 반환하거나 예외를 어떻게 처리할 수 있는지 알려주세요.

    댓글목록

    profile_image
    나우호스팅  1일 전



    ReflectionAttribute::getName을 사용하여 클래스의 속성을 가져올 때 속성이 없을 때 예외를 막으려면, 속성이 없을 때 null 또는 기본값을 반환하거나 예외를 처리하는 방법을 사용할 수 있습니다.

    속성이 없을 때 null을 반환하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    $reflectionAttribute = new ReflectionAttribute('속성명');
    
    $propertyName = $reflectionAttribute->getName();
    
    if (!$propertyName) {
    
        $propertyName = '기본값';
    
    }
    
    


    속성이 없을 때 기본값을 반환하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    $reflectionAttribute = new ReflectionAttribute('속성명');
    
    $propertyName = $reflectionAttribute->getName();
    
    $propertyName = $propertyName ?: '기본값';
    
    


    속성이 없을 때 예외를 처리하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    try {
    
        $reflectionAttribute = new ReflectionAttribute('속성명');
    
        $propertyName = $reflectionAttribute->getName();
    
    } catch (ReflectionException $e) {
    
        $propertyName = '기본값';
    
    }
    
    


    속성이 없을 때 null을 반환하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    $reflectionAttribute = new ReflectionAttribute('속성명');
    
    $propertyName = $reflectionAttribute->getName() ?? '기본값';
    
    


    속성이 없을 때 기본값을 반환하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    $reflectionAttribute = new ReflectionAttribute('속성명');
    
    $propertyName = $reflectionAttribute->getName() ?: '기본값';
    
    


    속성이 없을 때 예외를 처리하는 방법은 다음과 같습니다.

    #hostingforum.kr
    php
    
    try {
    
        $reflectionAttribute = new ReflectionAttribute('속성명');
    
        $propertyName = $reflectionAttribute->getName();
    
    } catch (ReflectionException $e) {
    
        $propertyName = '기본값';
    
    }
    
    

    2025-05-07 09:05

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

검색

게시물 검색