0

Let's say I have 3 scripts, the main/top one which includes second which in turn includes a third. Let me draw that so it be clear.

[top level script] -> [second level script] -> [third level script]

So, is there a easy way to get a name of the "second level script" from the third, maybe there's some predefined constant, does the third script included by the second one know its parent?

Please don't SCRIPT_NAME or PHP_SELF me, I know what is it and when I use it inside of a last included script it shows me the name of the "top level script". I need the name of a parent, not a grandfather!!!

  • Does this answer your question? [Get filename of file which ran PHP include](https://stackoverflow.com/questions/10393145/get-filename-of-file-which-ran-php-include) – Andrea Olivato Mar 17 '22 at 10:27
  • `debug_print_backtrace()` will show all scripts that were included – user3783243 Mar 17 '22 at 10:31
  • Well, according to documentation debug_print_backtrace() gives me too much data, I don't need that. get_included_files() is quite good... I'd prefer something even simpler, but I guess is what I have. About the link of Andrea, well if search well there, I probably could find there a phrase "You could also mess around with get_included_files()", but if you read my question and of this guy, you could probably notice the difference! That's why I said "Please don't SCRIPT_NAME or PHP_SELF me", which also means don't __FILE__ me! I wasn't asking the same question! – SizuNakomoto Mar 17 '22 at 13:54
  • So Andrea, I asked a specific question, your link has a general question, and so the answers there is "maybe this" or "maybe that" ... – SizuNakomoto Mar 17 '22 at 14:10
  • @SizuNakomoto The entire job of a programmer is to apply and adapt general techniques to specific scenarios, so you shouldn't expect the exact solution to be handed to you on a plate every time. It's fine to say that you couldn't work out how to apply the answers there to your case, but there's no need to be so outraged that someone pointed you in the direction of something they thought might help you. – IMSoP Mar 18 '22 at 11:14

1 Answers1

1

get_included_files should be useful. Place the following code in level 2 or level 3 file:

$files = get_included_files();
list($parent) = array_slice($files, -2, 1);
echo $parent;
Salman A
  • 262,204
  • 82
  • 430
  • 521
  • It would be useful to explain exactly what the `array_slice` is doing here - are you relying on the order of the returned array? Or knowing the exact length in advance? – IMSoP Mar 18 '22 at 08:50
  • Array slice to get second last item. – Salman A Mar 18 '22 at 10:22
  • Yes, but _why_ the second-to-last item? Is the order of that array guaranteed? What are the conditions that are required for this code to work? How could people adapt it to other situations? – IMSoP Mar 18 '22 at 11:04
  • @IMSoP yes the documentation does not mention guarantee of order but it seems to do the job just fine for the above particular example. If the files are included in another order (sequential for example) then a different approach is needed. – Salman A Mar 18 '22 at 11:08
  • 1
    @SalmanA sometimes you don’t need to explain anything, you just show an example and the guy who asks will figure it out and, based on your example, will build what he needs. That is, sometimes an example is a simple push in the right direction, so it's all good! – SizuNakomoto May 16 '22 at 10:55