Prefer using require_once or include_once

이 페이지는 아직 한국어로 제공되지 않으며 번역 작업 중입니다. 번역에 관한 질문이나 의견이 있으시면 언제든지 저희에게 연락해 주십시오.

Metadata

ID: php-best-practices/prefer-require-include-once

Language: PHP

Severity: Warning

Category: Error Prone

Description

This rule emphasizes the importance of using require_once or include_once instead of require or include in PHP. The main reason behind this is to avoid the inclusion of the same file more than once. If a file is included multiple times, it may lead to function redefinitions, variable value reassignments, and other unexpected behaviors which can be hard to debug.

require_once and include_once are statements that handle this problem effectively. They check if the file has already been included, and if so, they do not include it again. This ensures that the code within the file is executed only once, maintaining the integrity of your PHP application.

To comply with this rule, always use require_once or include_once when you want to include a PHP file. This will help to prevent potential issues with function redefinition or variable reassignment, and will make your code more reliable and easier to manage.

Non-Compliant Code Examples

<?php
require 'file.php';
include 'file.php';

Compliant Code Examples

<?php
require_once 'file.php';
include_once 'file.php';
https://static.datadoghq.com/static/images/logos/github_avatar.svg https://static.datadoghq.com/static/images/logos/vscode_avatar.svg jetbrains

Seamless integrations. Try Datadog Code Analysis

PREVIEWING: may/unit-testing