php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Doc Bug #80149 Mutually included files
Submitted: 2020-09-25 12:55 UTC Modified: 2020-09-26 23:12 UTC
From: marcio dot a dot siena at gmail dot com Assigned:
Status: Open Package: Documentation problem
PHP Version: Irrelevant OS:
Private report: No CVE-ID: None
 [2020-09-25 12:55 UTC] marcio dot a dot siena at gmail dot com
Description:
------------
The documentation does not talk about mutually included/required files.

Mutually included/required files: a.php requires (once) b.php, and b.php requires (once) a.php.

a.php is the "master" file. b.php is the included file. But what if b.php needs to re-include a.php?

Use case: legacy application being refactored, tons of cases. HTTP requests may come to a.php or to b.php, but both scripts depends upon each other.

When that occurs, as one may expected, a.php includes b.php, but the require_once in b.php does not re-include a.php.

But that behavior is not explicitly stated in the relevant documentation pages:

- https://www.php.net/manual/en/function.require-once.php
- https://www.php.net/manual/en/function.include-once.php

So one must make tests to be sure about PHP's true behavior.

My proposition: state that behavior clearly in the docs.


Test script:
---------------
- in a.php: require_once('b.php');

- in b.php: require_once('a.php');



Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2020-09-25 13:01 UTC] marcio dot a dot siena at gmail dot com
More complete scripts:

a1.php:
------

<?php

    echo "INSIDE a1.php <br><br>";
    require_once($_SERVER['DOCUMENT_ROOT'] . "/b1.php");

?>


b1.php:
------
<?php

    echo "INSIDE b1.php <br><br>";
    require_once($_SERVER['DOCUMENT_ROOT'] . "/a1.php");

?>
 [2020-09-25 13:05 UTC] danack@php.net
-Status: Open +Status: Feedback
 [2020-09-25 13:05 UTC] danack@php.net
"but the require_once in b.php does not re-include a.php. But that behavior is not explicitly stated in the relevant documentation pages:"


From the manual https://www.php.net/manual/en/function.require-once.php:
"The require_once statement is identical to require except PHP will check if the file has already been included, and if so, not include (require) it again."

That is pretty clear to me. If you don't think it is, can you suggest where and how it should be documented?
 [2020-09-26 12:05 UTC] marcio dot a dot siena at gmail dot com
Hi.

Sorry if I was not clear.

The main file, a1.php, is the file *requested* by the browser.

Then it includes b1.php. Only THEN, from b1.php, a1.php (the originally requested file) is included/required.
 [2020-09-26 19:38 UTC] requinix@php.net
The documentation does not say "...if the file has already been included by the use of include/require/include_once/require_once()".

The fact is that a1 was included at the beginning *by PHP*. And that counts. And if you dump get_included_files() inside a1 then you'll see a1 in the list of files.
 [2020-09-26 19:41 UTC] marcio dot a dot siena at gmail dot com
Ok, I will not argue.

If you have to do a get_included_files() instead of simply and clearly see it stated in the docs, so be it.

Thank you anyway.

Bye.
 [2020-09-26 19:50 UTC] requinix@php.net
I wasn't telling you to ragequit. I was saying that

> So one must make tests to be sure about PHP's true behavior.

the current behavior is correct.

@danack asked you

> [The doc is] pretty clear to me. If you don't think it is, can you suggest where and how it should be documented?

Are you still willing to answer that?
 [2020-09-26 22:25 UTC] marcio dot a dot siena at gmail dot com
Ok, thanks. Sorry for that.

Maybe add something like that:

- https://www.php.net/manual/en/function.require-once.php
- https://www.php.net/manual/en/function.include-once.php

"The script being requested by the user agent (browser) is considered an already included file, so include_once() and require_once() on it will have no additional effects. That can be veryfied by dumping the results of get_included_files()".

Thank you again.
 [2020-09-26 23:12 UTC] requinix@php.net
-Status: Feedback +Status: Open
 [2022-11-30 05:06 UTC] melindaetinw81 at gmail dot com
I was impressed by your writing (https://www.mycfavisit.biz/)github.com
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Nov 10 13:01:29 2024 UTC