Jump to content


Photo

Using too many includes/requires?


  • Please log in to reply
2 replies to this topic

#1 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 18 March 2006 - 03:22 AM

For my blog source, I have alot of custom functions that I need to include. It started to become a real pain so I've started to use one master include file that pulls in all the include files I need all at once. This seems like it would work, but having issues.

Here is my master include file:

<?php
#List all the files you wish to include...
$includes = array(
    'strings.inc.php',
    'test.inc.php'
);

require_once 'config.php';

foreach($includes as $include){
    require_once $include;
}

Now.. my config.php file is filled with constants. This is a fairly meaty file and really esentinal for the operation of the code. I call it first. Then I call all the includes in the array after that. Now, this setup works when I have like 3-4 include files and like 15 functions total. The more functions I have and the more files to include... I keep getting "Function 'test' is undefined" when it IS defined. If I include the file directly, it works just fine. Also, I call the config file before anything else, yet I can use the constants in there in other included files. The only solution I can see is to include the config.php for each include file. I tried that, and it still doesn't access the value for a constant.

Can someone help me please.

#2 fusionpixel

fusionpixel
  • Members
  • PipPipPip
  • Advanced Member
  • 64 posts

Posted 18 March 2006 - 08:20 AM

probably you are getting the undefines because there are functions runing before the variables needed they are loaded.

have you considered a "loader" file rather than an array?

<?

#file holder name holder.php

include ('file1.php');
include ('file2.php');
....

?>

then on your actual file

include ('holder.php');

Now, i havent tested this so I dont know if it will work.
Did you know there is a manual for PHP? [a href="http://" target="_blank"]http://www.php.net[/a]
Did you know there is a manual for mySQL? [a href="http://" target="_blank"]http://www.mysql.org[/a]

#3 maexus

maexus
  • Members
  • PipPipPip
  • Advanced Member
  • 191 posts

Posted 18 March 2006 - 04:24 PM

Well, I tried a simple test. I created a new test directory. In that dir, I created a dir called lib. In lib I have the master include file, as noted before. test.php and config.php.

test.inc.php

<?php
require_once 'config.php';
function test(){
    echo BASE_URL;
}
?>

config.php

<?php
define('BASE_URL', 'http://localhost/test');
?>

and finally index.php

<?php
require_once 'lib/master.php';
test();
?>

I get BASE_URL outputted and not [a href=\"http://localhost/test\" target=\"_blank\"]http://localhost/test[/a]. Now, if I add require_once 'lib/config.php'; to index.php along with the master file, it outputs the constant value. ~_~ This completely defeats the purpose of using the master include. I think the problem may be with how I setup require_once. I don't know. I've been trying to figure this out for a while now.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users