Auto loading classes and Namespacing inside a Local plugin

Re: Auto loading classes and Namespacing inside a Local plugin

by Michael Aherne -
Number of replies: 3
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

The most common problem I come across with class autoloading is that new classes don't start autoloading automatically (if that makes sense!). You need to go to /admin/index.php after adding a new class so that the class map will be refreshed. I've read that purging caches also does this but that has never worked for me.

Average of ratings: Useful (1)
In reply to Michael Aherne

This forum post has been removed

The content of this forum post has been removed and can no longer be accessed.
In reply to Deleted user

Re: Auto loading classes and Namespacing inside a Local plugin

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

There is a quick way to test if autoloading is fundamentally broken on your server. Visit any page in Moodle. If it doesn't instantly break, then class autoloading is working (this statement probably holds for most Moodle pages - I'm sure there must be some exceptions).

If you've added a new class inside classes/, named the file the same as the class and namespaced the class with {full name of plugin}\{name of subdirectory within classes} and purged the site caches then it will work. Just compare the name spacing you've used with that of any of the existing files in classes/ subdirectories within core Moodle plugins and see if you can work out what you have done differently (or stick a breakpoint just before you call the autoloading class and step through to see why the autoloader is failing).

In reply to Deleted user

Re: Auto loading classes and Namespacing inside a Local plugin

by Michael Aherne -
Picture of Core developers Picture of Peer reviewers Picture of Plugin developers

Autoloaded classes only appear in get_declared_classes() once they've been loaded, but that will only happen when you reference them in code. Are you doing that before calling get_declared_classes()?