Search code examples
phpphp-5.3php-5.4

How are include_once loops handled in php?


I have 3 different files-

  1. fileMainIncludeEverywhere.php

    ...
    include_once('fileMinorInclude.php');
    ?>
    
  2. fileMinorInclude.php

    ...
    include_once('fileMainIncludeEverywhere.php');
    ...
    
  3. fileToRun.php

    ...
    include_once('fileMainIncludeEverywhere.php');
    ...
    

I have a lot of files like fileToRun.php.

Currently I'm not facing any errors in my code, but I want to know if there's any case where this would fail?


Solution

  • I think no error in this case. Because include_once will only load the file for the first time then upcoming load request will be rejected for the same file.

    So from your example:

    1. fileToRun.php will load fileMainIncludeEverywhere.php (first call)
    2. fileMainIncludeEverywhere.php will load fileMinorInclude.php (first call)
    3. fileMinorInclude.php will call to load fileMainIncludeEverywhere.php but it will be rejected as it has been already loaded in first step.

    Hope it will help.