Jump to content

Can someone tell me why this file isn't being included when I call it?


maexus

Recommended Posts

So, I've been looking for a way to have a drag and drop type functionality for adding new includes for a script. Here is the basic dir struct for this:

 

[pre]

index.php

/lib/

  /plugins/

  /includes/

    page.class.php

  master.php

[/pre]

 

So, lib/master.php is responsible for including all required files and index.php includes lib/master.php. I have used this setup before and know it works. Before I basically put all the required includes file names in an array and foreach'd it to include them but then I decided to use glob() to creating a list of these automatically. It keeps the required maintenance to a minimum. Here is the code for that:

 

master.php:

<?php
$paths = array("includes", "plugins"); #the subdirectories in lib/
foreach($paths as $path){
$files = glob("{$path}/*.php");
foreach($files as $file){
	require_once($file);
}	
}
?>

 

Then I include lib/master.php on index.php and call a class from a include called in master.php. It said the class wasn't defined. So, I changed lib/master.php to:

 

<?php
$paths = array("includes", "plugins"); #the subdirectories in lib/
foreach($paths as $path){
$files = glob("{$path}/*.php");
foreach($files as $file){
	if(require_once($file)){
		echo "$file has been included";
	}
}	
}
?>

 

and I ran master.php directly. It outputted "includes/page.class.php was included". So, it's including it in lib/master.php but when I include lib/master.php in index.php, the page class is undefined.

 

Does anyone have advice on why this is not working?

Link to comment
Share on other sites

include() functions search for the path you provide based off the PHP include path. there are probably several paths in there but the one we care about is '.' or the current directory. So, for example, "includes/page.class.php". It is looking for that relative to the current directory, which is based on the main file that is being run, not the included file. Make sense?

 

Summary: So, since master.php is in the lib dir, it finds it. But if you try including master.php from index.php, it doesn't know to look in lib.

 

Solution: The fix is easy. We just need to figure out the absolute paths. Here is an updated master.php that should work:

<?php
$base = dirname(__FILE__); //Returns the directory that master.php is in
$paths = array("includes", "plugins"); #the subdirectories in lib/
foreach($paths as $path){
$files = glob("{$base}/{$path}/*.php");
foreach($files as $file){
	require_once($file);
}	
}
?>

 

Also, if you are using PHP5, you may want to check out the __autoload functionality: http://us2.php.net/autoload

Link to comment
Share on other sites

Also (and I know I'm just being picky now), but I would recommend ending your include files with a .inc instead of a .php. It helps with organization by signifying those files aren't scripts, but rather snippets of code.

 

Just my 2 cents...feel free to ignore me  :)

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.