Forum Moderators: coopster

Message Too Old, No Replies

Stop re-defining variables?

         

Ian Cunningham

4:24 pm on Apr 6, 2004 (gmt 0)

10+ Year Member



I am having major problems with a gallery I am trying to integrate fully with vbulletin.

All works well so far, apart from the templates!

In my gallery.php file I have

include("header.php")
*Gallery Code*
include("footer.php")

Somehow, only the includes after the gallery code are executed - anything before causes a redefining conflict within the gallery code. I have spent almost 10 hours trying to find where, but can't.

My question is, is there any way to clear all the variables and the functions in php? I would like the script to include header.php, then clear the variables/functions from that file and just keep the output from it.

Any help would be great, as I am REALLY stuck

coopster

5:10 pm on Apr 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Why not incorporate a couple of PHP functions to find the underlying issues?
include("header.php"); 
print '<pre>';
$arr = get_defined_vars() [php.net];
print_r($arr);
$arr = get_defined_functions() [php.net];
print_r($arr);
print '</pre><hr />';
*Gallery Code*
print '<pre>';
$arr = get_defined_vars() [php.net];
print_r($arr);
$arr = get_defined_functions() [php.net];
print_r($arr);
print '</pre><hr />';
include("footer.php");

Ian Cunningham

6:08 pm on Apr 6, 2004 (gmt 0)

10+ Year Member



Thanks, I'll give that a go now! I am a PHP novice, and I wouldn't have thought of something like that myself!

Just out of interest, is there any way to do what I described?

Ian Cunningham

6:36 pm on Apr 6, 2004 (gmt 0)

10+ Year Member



I gave that a go, and I used it to fix one bug which I found... but now I have this error:

Fatal error: Call to a member function on a non-object in C:\Server\htdocs\gallery.php on line 148

Can you explain what it means so I know how to fix it?

coopster

6:58 pm on Apr 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



In regards to your first question, you could get the defined information and then use the PHP unset() [php.net] function to get rid of unwanted variables and functions.

Let's discuss the variables first. Without some form of variable checking, you would be eliminating all variables, including the

$GLOBALS
for the rest of your script, which is probably not what you want happening! You would no longer have access to the
$_POST
,
$_GET
, etc. variables! Therefore, you will need to build some type of logic into the foreach [php.net] loop below to
unset
only those variables that you truly want unset.
$arr = get_defined_vars(); 
foreach (array_keys($arr) as $key) {
unset($arr[$key]); // WARNING: unsets ALL variables!
}

The user-defined functions are a bit easier to

unset
. PHP has separated them from the
internal
functions in a multi-dimensional array with a key of
'user'
.
$arr = get_defined_functions(); 
foreach (array_keys($arr['user']) as $function) {
unset($arr['user'][$function]);
}

coopster

7:06 pm on Apr 6, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



In regards to the second error, it seems as though it is trying to find an instance of a class and cannot. I would begin with line 148 and work your way back until you figure out what it is referencing...

Ian Cunningham

11:03 am on Apr 7, 2004 (gmt 0)

10+ Year Member



I have tried what you have suggested, but I can't find the overlapping variable as there are literally thousands in each script. (Vbulletin and 4Images global variables).

I have come across an interesting problem though when I include the files like this instead:

include("http://localhost/header.php")
*Gallery Code*
include("http://localhost/footer.php")

Adding the [localhost...] in seems to include all of the file fine, but it thinks I am "logged out" in the header/footer files - and doesn't remember my session.

Is there any way to find out names of conflicting variables with a script?

ergophobe

4:20 pm on Apr 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



One way is to find a debugger and step through your code a line at a time and watch what's happening to the variables. This could be pretty time consuming depending on which variables you're trying to watch. Not to mention that I haven't really had much luck getting the PHP debuggers to work. Other people do, though, so I'm sure it's my fault...

I have gotten xdebug [xdebug.org] to work, but haven't really used it for debugging. You might get what you need from doing a stack trace (see the documentation). You may well need to debug interactively, watching specific variables, and that might take some time. One thing that's not particularly clear from the Xdebug site is that the debugger client for interactive debugging uses an interface based on the GDB debugger, which is documented here [gnu.org]

Other debuggers,

- one is packaged with ActiveState Kommodo - this one worked great for me way back when, but I haven't had a working copy of Komodo in a long time (and it costs money).

- dbg [dd.cron.ru] - I've never managed to get this one to work at all.