This is boggling my mind. I wrote a quick auto-loader and it appears to work like it is supposed to, except that it keeps changing the argument $class from the class name to "self". Here is the code (followed by further information on what I've tried):

public static function load($class) {
		$found = false;
		foreach (self::$includePaths as $path) {
			if (file_exists(ROOT . $path . DS . $class . '.php')) {
				require_once(ROOT . $path . DS . $class . '.php');
				$found = true;
				break;
			}
		}

		if (!$found) {
			die($class . ' not found');
		}
	}

Okay so basically, if a class can't be loaded I want to know what class it was so I die with a message containing the contents of $class. If I write "echo $class;" before the red if block it works fine and echos the class name. However, for some reason if I try to echo $class within that if block or after it inside of the method it changes the contents of $class to "self". var_dump($class) returns string(4) "self". Is there a reason it is randomly changing the contents of my $class variable!?!? I've tried various other methods including unsetting the path when the class file is found followed by an if (isset($path)) { die($class . ' not found'); } but still no dice, it changes the contents of $class to self. I've been banging my head off this problem for hours now with no success, so if anyone has any feedback or suggestions at all I would love you forever.

Thanks!

So it turns out that the autoloader was getting called on "self" for some reason. Perhaps it forgot that self:: isn't a class, but rather a keyword. Either way, a simple if ($class == "self") return; took care of that problem. God, I need to sleep. >.>

Solution: (Don't judge me :))

public static function load($classname) {
		if ($classname == "self") return;

		foreach (self::$includePaths as $path) {
			if (file_exists(ROOT . $path . DS . $classname . '.php')) {
				require_once(ROOT . $path . DS . $classname . '.php');
				unset($path);
				break;
			}
		}

		if (isset($path)) {
			die("Autoloader could not locate: $class");
		}
	}
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.