If I have a file a.php which I can’t edit.
<?php
$a = 1;
function a() {
global $a;
echo $a;
}
a();
then running php a.php prints 1 nicely. But if I have b.php:
<?php
function b() {
include "a.php";
}
b();
Then running php b.php doesn’t print anything.
What can I type before the include "a.php" to make it behave the same without editing a.php?
(Obviously other than defining $a. In my real-world example it has to work for a complicated a.php).
It sounds like
a.phpis some legacy or 3rd party script that you have to wrap in your own application. I had a similar problem once and the solution was not pretty. First, you will have to find out, what global variables exist (grep globalor similar helps).This is the relevant part of my “Script Adapter” class:
As you see, I had to catch the output too. The class also emulates
$_GETand catchesheadercalls to assign everything to aResponseobject, but for your case only this method is important. It gets called with the file name and an array of global variable names as parameters: