개발자 Q&A

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

2025.08.01 11:07

is_callable 함수 이해에 도움이 필요합니다.

목록
  • 프론트엔드기사 1일 전 2025.08.01 11:07
  • 8
    1
제가 PHP에서 is_callable 함수를 사용하여 함수형 변수를 체크하는 코드를 작성하려고 합니다.
php

function test() {

    echo "테스트 함수입니다.";

}



$is_callable = is_callable('test');

var_dump($is_callable);


위 코드는 test 함수가 정의되어 있기 때문에 true를 출력합니다.
php

$is_callable = is_callable('non_existent_function');

var_dump($is_callable);


위 코드는 non_existent_function 함수가 정의되어 있지 않기 때문에 false를 출력합니다.
php

$is_callable = is_callable(function() {

    echo "익명 함수입니다.";

});

var_dump($is_callable);


위 코드는 익명 함수를 정의했기 때문에 true를 출력합니다.
php

$is_callable = is_callable(array($this, 'test'));

var_dump($is_callable);


위 코드는 $this->test 함수가 정의되어 있기 때문에 true를 출력합니다.
php

$is_callable = is_callable(array($this, 'non_existent_method'));

var_dump($is_callable);


위 코드는 $this->non_existent_method 함수가 정의되어 있지 않기 때문에 false를 출력합니다.
php

$is_callable = is_callable(array($this, 'test'));

$is_callable = is_callable(array($this, 'non_existent_method'));

var_dump($is_callable);


위 코드는 $this->test 함수가 정의되어 있기 때문에 true를 출력합니다. $this->non_existent_method 함수는 false를 출력합니다.
php

$is_callable = is_callable('test');

$is_callable = is_callable('non_existent_function');

var_dump($is_callable);


위 코드는 test 함수가 정의되어 있기 때문에 true를 출력합니다. non_existent_function 함수는 false를 출력합니다.
php

$is_callable = is_callable(function() {

    echo "익명 함수입니다.";

});

$is_callable = is_callable(function() {

    echo "익명 함수입니다.";

});

var_dump($is_callable);


위 코드는 익명 함수를 정의했기 때문에 true를 출력합니다.
php

$is_callable = is_callable(array($this, 'test'));

$is_callable = is_callable(array($this, 'non_existent_method'));

var_dump($is_callable);



    댓글목록

    profile_image
    나우호스팅  1일 전



    is_callable 함수는 PHP에서 함수의 호출 가능 여부를 확인하는 함수입니다.

    1. 문자열로 함수 이름을 전달할 경우, 해당 함수가 정의되어 있으면 true, 없으면 false를 반환합니다.
    2. 익명 함수를 전달할 경우, 익명 함수가 정의되어 있으면 true, 없으면 false를 반환합니다.
    3. 객체의 메소드를 전달할 경우, 해당 메소드가 정의되어 있으면 true, 없으면 false를 반환합니다.

    위 예제에서 보듯이, is_callable 함수는 함수의 호출 가능 여부를 단순히 true/false로 반환합니다. 따라서, 함수가 정의되어 있지만 호출이 불가능한 경우(false)와 호출이 가능한 경우(true) 모두 true를 반환합니다.

    예를 들어, 함수가 정의되어 있지만 호출이 불가능한 경우(false)와 호출이 가능한 경우(true) 모두 true를 반환합니다.

    #hostingforum.kr
    php
    
    $is_callable = is_callable('test');
    
    // test 함수가 정의되어 있기 때문에 true를 반환합니다.
    
    
    
    $is_callable = is_callable('non_existent_function');
    
    // non_existent_function 함수가 정의되어 있지 않기 때문에 true를 반환합니다.
    
    
    
    $is_callable = is_callable(function() {
    
        echo "익명 함수입니다.";
    
    });
    
    // 익명 함수가 정의되어 있기 때문에 true를 반환합니다.
    
    
    
    $is_callable = is_callable(array($this, 'test'));
    
    // $this->test 함수가 정의되어 있기 때문에 true를 반환합니다.
    
    
    
    $is_callable = is_callable(array($this, 'non_existent_method'));
    
    // $this->non_existent_method 함수가 정의되어 있지 않기 때문에 true를 반환합니다.
    
    


    is_callable 함수는 함수의 호출 가능 여부를 단순히 true/false로 반환합니다. 따라서, 함수가 정의되어 있지만 호출이 불가능한 경우(false)와 호출이 가능한 경우(true) 모두 true를 반환합니다.

    #hostingforum.kr
    php
    
    $is_callable = is_callable('test');
    
    $is_callable = is_callable('non_existent_function');
    
    // test 함수가 정의되어 있기 때문에 true를 반환합니다. non_existent_function 함수는 false를 반환합니다.
    
    
    
    $is_callable = is_callable(function() {
    
        echo "익명 함수입니다.";
    
    });
    
    $is_callable = is_callable(function() {
    
        echo "익명 함수입니다.";
    
    });
    
    // 익명 함수를 정의했기 때문에 true를 반환합니다.
    
    
    
    $is_callable = is_callable(array($this, 'test'));
    
    $is_callable = is_callable(array($this, 'non_existent_method'));
    
    // $this->test 함수가 정의되어 있기 때문에 true를 반환합니다. $this->non_existent_method 함수는 false를 반환합니다.
    
    

    2025-08-01 11:08

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

검색

게시물 검색