I have 2 files: master.php and test.php. master.php has a class in it that I would like to include in test.php. The only problem is that test.php is ignoring the class in master.php.

Here is the code:

Test.php:

include("http://".$_SERVER['HTTP_HOST']."/rebuild/scripts/php/master.php");
$loader = new modLoader();
$module[0] = 'm.class.templating.php';
$module[1] = 'm.class.userInfo.php';
$loader->modules = $module;
$loader->loadUp();

master.php:

class modLoader
	{
		public $modules = array();
		
		function loadUp()
			{
				include('http://'.$_SERVER['HTTP_HOST'].'/rebuild/scripts/php/classes.php');
				
				foreach($this->modules as $key=>$value)
					{
						include('http://'.$_SERVER['HTTP_HOST'].'/rebuild/scripts/php/modules/'.$value);
					}
			}
	}

Here is the error I get:
Fatal error: Class 'modLoader' not found in /var/www/html/rebuild/test.php on line 3

I've even tried having the include file master.php echo some stuff and that works fine. Just no class. Anyone have any ideas?

Dani AI

Generated

As discovered, the root cause was including the file with an HTTP URL instead of loading it from the filesystem. When you include a PHP file via an HTTP URL the webserver executes that file and returns its output (HTML/text) — not the raw PHP source or class declarations — so any class definitions never get defined in your current script. That also explains why echo in the included file appeared to work while new modLoader() failed. ’s scope idea is a reasonable check, but the symptoms point to the URL-include behavior instead.

Fixes and better practices:

  • Include from the filesystem (relative or absolute), not via http://. Prefer require_once so missing files throw a fatal error instead of failing silently:
require_once __DIR__ . '/rebuild/scripts/php/master.php';
$loader = new modLoader();
  • For larger projects, use an autoloader (or Composer/PSR-4) so class files are pulled automatically:
spl_autoload_register(function($class){
    $f = __DIR__ . '/classes/' . $class . '.php';
    if (file_exists($f)) require_once $f;
});

Troubleshooting tips and cautions:

  • Check what was actually included with var_dump(get_included_files());.
  • Verify allow_url_include and allow_url_fopen if you suspect URL includes (they are off by default for security): var_dump(ini_get('allow_url_include'), ini_get('allow_url_fopen'));.
  • Do not enable allow_url_include on production servers — it’s a serious security risk. Use filesystem paths and __DIR__/realpath() to make includes reliable and portable.

Recommended Answers

All 2 Replies

I have, unfortunately, no experience with OOP, and can't make sense of OOP code, but this sounds like a scope issue to me.

You've established that the master.php code is being referenced. That would have been my first piece of advice.

How about if you put your echo test statement inside the class? Does it work outside the class but fail inside? If so, and it was a function displaying that behavior, the culprit would be scope and it could be fixed with a simple global $myvar; statement.

Could that fix your problem?

(I've taken courses, and understand the basic OOP concepts, but can't see how to actually apply it to my PHP projects. Too bad, enough people embrace it that I know it must be worthwhile. Some day maybe.)

Blah! Nevermind, I found out that it was because I was including the full non relative path.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.