Jump to content

Archived

This topic is now archived and is closed to further replies.

maexus

Using too many includes/requires?

Recommended Posts

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:

[code]
<?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;
}
[/code]

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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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

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

config.php

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

and finally index.php

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

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.

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.