PHpullh
학습 라이브러리/PHP/function_exists 폴리필

PHP · 함수

function_exists 폴리필

폴리필을 안전하게 선언하기 위해 function_exists를 감싸는 예제입니다.

함수중급polyfillfunction_existscompatibility

핵심 설명

폴리필을 안전하게 선언하기 위해 function_exists를 감싸는 예제입니다.

PHP code

<?php
if (!function_exists('str_contains_all')) {
    function str_contains_all(string $text, array $needles): bool {
        foreach ($needles as $needle) {
            if (!str_contains($text, $needle)) {
                return false;
            }
        }
        return true;
    }
}

var_dump(str_contains_all('php api server', ['php', 'api']));

학습 팁

함수 예제는 인자 타입, 반환 타입, 호출 방식 세 가지를 같이 확인하면 이해가 빠릅니다.

주의할 점

인자 순서와 기본값, 참조 전달 여부를 혼동하면 함수 호출이 쉽게 꼬입니다.

자주 묻는 질문

function_exists 폴리필란 무엇인가요?

폴리필을 안전하게 선언하기 위해 function_exists를 감싸는 예제입니다.

function_exists 폴리필 학습 시 주의할 점은 무엇인가요?

인자 순서와 기본값, 참조 전달 여부를 혼동하면 함수 호출이 쉽게 꼬입니다.