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

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
jgarbers
Forum Newbie
Posts: 5
Joined: Wed Jan 05, 2005 9:55 am

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

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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..
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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";
}
jgarbers
Forum Newbie
Posts: 5
Joined: Wed Jan 05, 2005 9:55 am

Post 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?
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post 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).
jgarbers
Forum Newbie
Posts: 5
Joined: Wed Jan 05, 2005 9:55 am

Post 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!
Post Reply