php variables not passing, I think the scope changed?

amber ɹəqɯɐ

New member
I am adding some debug output functions to a PHP application.

Everything worked well before I changed anything.

I created a wrapper function for the require() function called a_require().
a_require() takes some debug info and echos it if debug is on. Then it sends the file name to require().

My problem is this:
require(DIR_LANGS . $lang . '/index.php');
I started out with this, and it took all the variables and ended up with the correct path.

I changed it to this:
a_require(DIR_LANGS . $lang . '/index.php',__LINE__,__FILE__);

My wrapper function is like this:
function a_require ($required, $line, $file){
global $debug;
if ($debug == 'y') {echo('<div class="debug">From line '.$line.' of '.$file.' - require '.$required.'</div>');}
require($required);
}

Soooo, after just doing this, the variable $lang is empty now.
I understand that I may have messed with the scope of $lang.
$lang is defined in app_start.php which is required inside index.php (the file where my trouble lies)
$lang is properly set all the way to the end of app_start.php

I believe the problem is that before I changed require() to a_require(), app_start.php was part of the global scope.

But now, it is required from inside the function a_require(), which means everything declared inside app_start.php becomes local.

Am I correct?

If so, how do I overcome this?
Okay, duh.

Before $lang is defined, I added
global $lang;

I guess I will have to do that for all the global vars if I wish to use this wrapper function

>:-(

Unless... anyone out there have a more broad solution for me?

I suppose I could just interject with an echo function before each require and include, rather than wrapping the requires and includes...

I am having fun learning php. Even though I solved this one bug, I would love to hear more suggestions if there is something I have not thought of.

Thanks!
 
Back
Top