Check if a file was included or loaded

后端 未结 12 1216
轻奢々
轻奢々 2020-12-08 13:35

Is there any elegant way to check if a file was included by using include/include_once/require/require_once or if the pag

12条回答
  •  佛祖请我去吃肉
    2020-12-08 14:04

    I took a similar approach to this issue when I cam across it. The solution I found was to load each file as needed in an include_once method. Hope this helps.

    $FILES = get_included_files();  // Retrieves files included as array($FILE)
    $FILE = __FILE__;               // Set value of current file with absolute path
    if(!in_array($FILE, $FILES)){   // Checks if file $FILE is in $FILES
      include_once "PATH_TO_FILE";  // Includes file with include_once if $FILE is not found.
    }
    

    I have the following function established to check files loaded:

    ARRAY_DUMP($FILES);
    
    function ARRAY_DUMP($array){
      echo "
        ".date('h:i:s').":
        
    ", print_r($array, 1), "
    "; }

    Output:

    currentArray
    (
      [0] => /home/MY_DOMAIN/hardeen/index.php
      [1] => /home/MY_DOMAIN/hardeen/core/construct.php
      [2] => /home/MY_DOMAIN/hardeen/core/template.php
      [3] => /home/MY_DOMAIN/hardeen/bin/tags.php
      [4] => /home/MY_DOMAIN/hardeen/bin/systemFunction.php
    )
    

提交回复
热议问题