Need help with Scope, I think

Hi,
I’m having problems referencing an array in my php file. This is an included file. The array is declared and initialize outside of any functions called. I wanted to use the array as a lookup table for file locations. however, from within a function the array is undefined. Maybe I’m just not understanding scope in php. Anyway, here is some sample code:

$sysfiles = array(
“notify” => dirname($_SERVER[‘DOCUMENT_ROOT’]).DIRECTORY_SEPARATOR.“appFiles”.DIRECTORY_SEPARATOR.“notifyendpoint.txt”,
“client” => dirname($_SERVER[‘DOCUMENT_ROOT’]).DIRECTORY_SEPARATOR.“appFiles”.DIRECTORY_SEPARATOR.“clients.txt”,
);

if(!function_exists(‘getfilecontents’)){
function getfilecontents(string $file){
$filename = $sysfiles[$file];
return preg_replace(’/[[:cntrl:]]/’, ‘’, $file);
}
}

Within this test code(Which I know does nothing yet) I get an undefined variable error when I reference $sysfiles. It is declared and I can print_r it from outside the function.

Any thoughts?

Thanks

Well, I think I asked to quick. I found the global keyword that I used in the function. Seems to work. But if more experienced would like to make suggestions, that would be great.

All input data to a function should be supplied as call-time parameters, so that the function is general-purpose and reusable. By using the global keyword to make the function ‘work’, the function is tied to operating on specific data and you must now also know about the internal operation of the function in order to use it, requiring more work on your part, not less work. Functions written like this have the wrong responsibility and are just part of your main application code with an extra function definition and function call added to what the code was doing.

1 Like

Thank you for the insight!
So my desire is to not have to redefine the same paths over and over again. How would you do that?
If you use information stored in an ini file, that’s great, but that means reading and parsing a file over and over again. I can do that, certainly you probably would need to do that when it comes to you database credentials. I come from a c# background, not the stateless environment of web development and now I’m learning php as the backend. So I get a little confused.

I do agree you should pass parameters into functions. The calling code would then also need to know about the lookup table. Where would it get the information unless the lookup table is declared as a Super Global. Should I have a file with SuperGlobal declarations included in all my models? If I dedicate a php file to declaring super globals, I might as well have individual global constants rather that a lookup table. On the other hand, if I ever wanted to work with all my paths, an array is much easier to move around.(for instance print_r an array is easier than print_r each constant.)

Please be patient, I have some experience in other programming structures, but again new to php.

A more straightforward approach might be to use constants:

define('SYSFILE_NOTIFY', dirname($_SERVER['DOCUMENT_ROOT']).DIRECTORY_SEPARATOR.'appFiles'.DIRECTORY_SEPARATOR.'notifyendpoint.txt');
define('SYSFILE_CLIENT', dirname($_SERVER['DOCUMENT_ROOT']).DIRECTORY_SEPARATOR.'appFiles'.DIRECTORY_SEPARATOR.'clients.txt');

function getfilecontents(string $filepath) {
  ...
}

$contents = getfilecontents(SYSFILE_NOTIFY);

This prevents most of the problems with globals - you can’t change a constant, so your code always knows what it’s dealing with. Your editor may also understand constant definitions better than associative arrays, meaning it can help complete the constant name when calling your function.

Thank you for the Suggestion and the info about Constants being recognized easier at call time! I’m going to implement this technique.

Sponsor our Newsletter | Privacy Policy | Terms of Service