Is there any way to find out which FILE used require_once?

Let's say I have the following situation:

File1.php:

<?php require_once('init.php'); ... ?> 

File2.php:

 <?php require_once('init.php'); ... ?> 

init.php:

 <?php magic_function_which_tells_me_which_file_parsed_this_file(); ... ?> 

I know this is a long snapshot, but is there a way to find out inside init.php which file included init.php in the current execution?

+7
php require-once
source share
5 answers

You can use debug_backtrace to find the caller even without functions:

test1.php

 <?php echo 'test1'; include 'test2.php'; 

test2.php

 <?php echo 'test2'; print_r(debug_backtrace()); 

Exit

 ABCArray ( [0] => Array ( [file] => /tmp/b.php [line] => 3 [function] => include ) [1] => Array ( [file] => /tmp/a.php [line] => 3 [args] => Array ( [0] => /tmp/b.php ) [function] => include ) ) 

In any case, I would not recommend using it, because there can be a noticeable drag and drop of performance when overused.

+4
source share

At the top of init.php, you can use debug_backtrace() to get stack information. This will tell you, among other things, which file included the current file and on which line.

This is a sample backtrace output. If you put this in a function, you get another layer of data. If you name it correctly in the file itself, then the topmost layer will tell you which file included this file.

 array (size=2) 0 => array (size=3) 'file' => string 'fileThatIncudedMe.php' (length=63) 'line' => int 6 'function' => string 'require_once' (length=12) 

You can translate this into a utility function:

 function whoIncludedThisFile() { $bt = debug_backtrace(); $includedMe = false; while (count($bt) > 0) { $set = array_shift($bt); if ( array_key_exists('function', $set) === true && in_array($set['function'], array('require', 'require_once', 'include', 'include_once')) ){ $includedMe = array('file'=>$set['file'], 'line'=>$set['line']); break; } } return $includedMe; } print_r(whoIncludedThisFile()); // Array ( [file] => topLevelFile.php [line] => 2 ) 
+3
source share

Of course. With debug_print_backtrace() .

# 0 require_once () called in [C: \ xampp \ htdocs \ file2.php: 3]

# 1 require_once (C: \ xampp \ htdocs \ file2.php), called in [C: \ xampp \ htdocs \ file1.php: 3]

This will tell you that init.php was included from file2.php on line 3 .

+3
source share

You can also try to use a variable to achieve this. Name it $ parentFile:

 $parentFile = basename(__FILE__); require('some.file.here.php'); 

And in the file some.file.here.php:

 if($parentFile == 'another.file.php') // do something; 
0
source share

I will figure out the answer - obviously, all the services to the guys who have already answered this in front of me.

I did to format the debug_backtrace output in the error log:

 $debug = debug_backtrace(2 , 16); error_log('-------------------------------' ); foreach ( $debug as $error ) { error_log( str_pad( $error['file' ], 120 ) . str_pad($error ['line'] , 8) . $error['function' ] ); } 

The result will be one file per line containing (file, line, function) in the table.

0
source share

All Articles