0

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!

1
Contributor
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Kognition
0

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");
		}
	}

Edited by Kognition: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.