-3

I'am asking myself why should i use include or include_once.

I know there is a question about it, but i dont find my answer in it.

In my case my problem is a little bit different, but it can summarize as following. If i use 4 files who includes themselves, by example:

A includes D

B includes C,D

C no include

D includes C

Should i use "include" or "include_once", and why ?

EDIT

Little mistake in the problem. I just rewrite.

EDIT

To explain my self, D need C to works, but C does not need for D to works. Sometime i have to include C, sometimes i have to include D. D has been refactored, so now it includes C automatically, but there is more than 2 thousand files to review in many directories... so i'am wondering if i use a "include_once" in D is better to use a "include" in D.

Community
  • 1
  • 1
Xavier S.
  • 1,147
  • 13
  • 33
  • 2
    Because B includes D, then D includes B, if you just let it include every time you'll keep loading B, D, B, D, B, D... until you run out of memory. It just won't work – Joe Mar 28 '13 at 14:17
  • 1
    You can find the answer by searching the difference between `include` and `include_once` in Google... Lot of topics. (Prefer `include_once` or `require_once` ^^) – JoDev Mar 28 '13 at 14:17
  • This is no longer recursive after your edit. – james_t Mar 28 '13 at 14:27
  • I dont ask the difference between include and include_once. I need to know, if in this case, i have to use include or include_once. – Xavier S. Mar 29 '13 at 09:20
  • @JoDev Indeed... recursive is not the good word... but i might use "included more than one time" – Xavier S. Mar 29 '13 at 09:41
  • And my answwer is : prefer to use `_once` for includes or requires... whatever the context is, `_once` will avoid a file to be loaded twice! And to complete my answer, in B include C isn't needed because D will already load C. – JoDev Mar 29 '13 at 09:47

2 Answers2

0

You should use include_once, otherwise, when loading B, you would be loading D, then B again, then D again, and so on forever (or, more acuratelly, until you run out of memory).

Étienne Miret
  • 6,448
  • 5
  • 24
  • 36
  • That's not quite correct. This will only happen if there are no naming conflicts with the code. A duplicate function name or class name in the same namespace will throw a fatal error and stop execution. – james_t Mar 28 '13 at 14:26
  • Right, but includes are usually at the begining of a file, before any function or class definitions (mines are, at least). – Étienne Miret Mar 28 '13 at 14:39
  • No, the position of the include has nothing to do with the scope of the functions defined. "When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward. However, all functions and classes defined in the included file have the global scope." [phpdoc](http://php.net/manual/en/function.include.php). – james_t Mar 28 '13 at 15:23
  • I know that. My point was that if the includes are at the begining of the files, the classes and functions definitions are never read. – Étienne Miret Mar 28 '13 at 15:41
  • test.php -> ` ` – james_t Mar 28 '13 at 15:47
  • Also note the maximum nesting level, which prevents unintended infinite loops `PHP Fatal error: Maximum function nesting level of '#' reached` – james_t Mar 28 '13 at 16:00
0

Use *_once. Having so many "weird" includes makes me wonder if your programming logic is still valid. I recommend to use *_once in combination with an autoloader if possible.

Anyone
  • 2,814
  • 1
  • 22
  • 27