-
Notifications
You must be signed in to change notification settings - Fork 47
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Autoloader: Use a base dir #66
Comments
Can you give an example? You can easily load classes as long as the structure adheres to the autoloader's folder structure, which is If you look at line 36 on $filename = __DIR__ . '/' . str_replace( '\\', '/', $class ) . '.php'; Something that needs to change is that the autoloader shouldn't throw an Exception if the file isn't loaded, as that stops other auto-loaders from being registered and used as a lower priority than the one defined here (composer etc)... This is something that I have changed in my new re-write of the bot (soon to have an initial release on my [ElvenSpellmaker's] fork). If you really can't load the class, then you could always use |
Yes, there's the problem as well. DIR points at the directory the file currently in context resides in. So if you have Bot.php, which is in Classes/Library/IRC, DIR is Classes/Library/IRC. ETA: DIR = _ _ DIR _ _ without spaces. I suck at Markdown. |
The autoloader is perfectly fine, it points to _Classes/_ which is where all classes should be? It points to where the |
Nevermind, wasn't thinking. Either way, if the autoloader is fine, please explain why this doesn't work:
|
I assume you're trying to use the built in namespace Foo;
// Looks for Foo\Exception
$foo = new Exception;
// Looks for Exception in the global namespace.
$foo = new \Exception;
// If Exception is not defined in Foo\Exception then you can alias it using a `use` statement
// Notice the lack of \ in front of the Exception as use statements always start from the gobal namespace.
use Exception;
// Now looks for \Exception, not \Foo\Exception
$foo = new Exception. Generally people import/alias classes from different namespaces to make it cleaner code and to be able to switch the class out with just editing the use statement, rather than everywhere it might be in the code. To fix this then you can do one of two things:
Also the bot doesn't use any (You can also alias to other class names and more with namespaces). Check out: http://php.net/manual/en/language.namespaces.php |
I just copied over the quoted code from the bot, same file; I don't see why
|
Right now the autoloader freaks out if it doesn't find the file as it expects every class to be loaded from within the phpbot404.php file. If you want to initialise a class from a command, it won't work; the autoloader will try to load an inexisting file and crash.
Solution: Set a 'dir' => dirname(FILE) var in config.php and make the autoloader use that as prefix.
The text was updated successfully, but these errors were encountered: