Page 1 of 1

Appropriate place to 'include' or 'require' code?

Posted: Thu Jan 06, 2005 12:26 pm
by jgarbers
Another newbie question, this perhaps more about style and best practices. Say I have a function xample() that requires support from code that lives in another file, helper.php. I can do this:

Code: Select all

function xample() {
  require_once 'helper.php';
  echo 'Do something with helper.';
}
where the require_once is inside the function definition, or

Code: Select all

require_once 'helper.php';
function xample() {
  echo 'Do something with helper.';
}
putting the require_once first. I've seen examples of both techniques. However, I've noticed a problem with an "internal" include - if the included code declares a global variable, the function won't be able to access it as a global because it was actually declared within the functions own scope.

Example helper.php:

Code: Select all

$HELPER = "XYZZY";
function helper_function() {
	echo "blah blah";
}
and if I use this:

Code: Select all

function xample() {
  require_once 'helper.php';
  global $HELPER;
  echo 'The value is $HELPER";
}
I don't get The value is XYZZY, I get The value is.
Am I interpreting this right? What are the implications of choosing one over the other? I'm coming from a C/C++ background and this on-the-fly compilation / interpretation stuff is new to me.

Thanks in advance.

Posted: Thu Jan 06, 2005 12:39 pm
by feyd
including/requiring inside a function only allows setting of variables and running code of sorts. You can't declare any functions or other things. While you can do anything if you include/require outside a function. Typically, use of globals should be avoided at all costs. Superglobals are okay, but should be used sparingly. Reason being, you don't have a very generic function if it uses stuff you didn't pass into it through various means..

Posted: Thu Jan 06, 2005 12:47 pm
by markl999
I prefer the 'lazy loading' method of putting the require inside the function. In your example above you don't need 'global $HELPER' as this 'pulls' a variable into scope, but $HELPER is already in scope. So the following works ok:

Code: Select all

function xample() {
  require_once 'helper.php';
  //global $HELPER;
  echo "The value is $HELPER";
}

Posted: Thu Jan 06, 2005 1:14 pm
by jgarbers
Hmm, okay... but if I want to use $HELPER to store global configuration information, your approach wouldn't give me the results I'd want, right? Each function that included helper.php would have its own, different copy of $HELPER?

Posted: Thu Jan 06, 2005 1:21 pm
by markl999
You've almost answered your own question there ;)
store global configuration information
For 'global' info you could use the $GLOBALS superglobal, e.g
helper.php

Code: Select all

$GLOBALS['HELPER'] = 'XYZZY';
function helper_function(){
  echo 'blah blah';
}
test.php

Code: Select all

function xample() {
  require_once 'helper.php';
  echo 'The value is '.$GLOBALS['HELPER']; //echo's XYZZY
}
xample();
echo $GLOBALS['HELPER']; //also echo's XYZZY
Course, if the data really is global and is to be used 'everywhere' not just inside the xample() function then you're better off require'ing helper.php outside the function (or maybe using auto_prepend_file) so that $HELPER is always in scope (in this case you wouldn't need to use $GLOBALS).

Posted: Thu Jan 06, 2005 1:29 pm
by jgarbers
Great tips - thanks! It hadn't occurred to me to store things in $GLOBALS - shows you how new I am at this. To keep the code cleaner-looking, I think I'll go with the include-outside-the-function approach instead. Thanks again!