2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-05-11 14:02:13 +02:00
At root, ``++require++``, ``++require_once++``, ``++include++``, and ``++include_once++`` all perform the same task of including one file in another.
However, the way they perform that task differs, and they should not be used interchangeably.
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
* ``++require++`` includes a file but generates a fatal error if an error occurs in the process.
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
* ``++include++`` also includes a file, but generates only a warning if an error occurs.
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
Predictably, the difference between ``++require++`` and ``++require_once++`` is the same as the difference between ``++include++`` and ``++include_once++``.
2023-10-30 10:33:56 +01:00
The ``++_once++`` versions ensure that the specified file is only included once.
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
=== What is the potential impact?
2021-04-28 16:49:39 +02:00
2023-10-30 10:33:56 +01:00
Including the same file multiple times could have unpredictable results, the ``++_once++`` versions are preferred.
2023-05-11 14:02:13 +02:00
Additionally, as ``++include_once++`` generates only warnings, it should be used only when the file is being included conditionally, i.e. when all possible error conditions have been checked beforehand.
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
== How to fix it
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
Rewrite your code to only contain `require_once` and conditional `include_once` calls.
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
=== Code examples
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-05-11 14:02:13 +02:00
[source,php,diff-id=1,diff-type=noncompliant]
2021-04-28 16:49:39 +02:00
----
include 'code.php'; //Noncompliant; not a "_once" usage and not conditional
include $user.'_history.php'; // Noncompliant
require 'more_code.php'; // Noncompliant; not a "_once" usage
----
2023-05-11 14:02:13 +02:00
==== Compliant solution
2021-04-28 18:08:03 +02:00
2023-05-11 14:02:13 +02:00
[source,php,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
require_once 'code.php';
if (is_member($user)) {
include_once $user.'_history.php';
}
require_once 'more_code.php';
----
2021-04-28 18:08:03 +02:00
2023-05-11 14:02:13 +02:00
== Resources
=== Documentation
2021-04-28 18:08:03 +02:00
2023-05-11 14:02:13 +02:00
* https://www.php.net/manual/en/function.require-once.php[PHP Manual - require-once]
* https://www.php.net/manual/en/function.require.php[PHP Manual - require]
* https://www.php.net/manual/en/function.include-once.php[PHP Manual - include-once]
* https://www.php.net/manual/en/function.include.php[PHP Manual - include]
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-09-20 15:38:42 +02:00
'''
2023-05-11 14:02:13 +02:00
2021-09-20 15:38:42 +02:00
== Implementation Specification
2023-05-11 14:02:13 +02:00
2021-09-20 15:38:42 +02:00
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
Replace "xxx" with "yyy".
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2023-05-11 14:02:13 +02:00
2021-06-02 20:44:38 +02:00
== Comments And Links
2023-05-11 14:02:13 +02:00
2021-06-02 20:44:38 +02:00
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 19 May 2015, 15:51:55 Linda Martin wrote:
Reviewed
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]