If if worked with concrete5 before, you’ll probably have seen lots of Loader::model or Loader::helper. A few versions ago these calls were pretty hard to avoid and you had to use them rather often. The core still doesn’t have namespaces due to the fact that the development of concrete5 has started when PHP didn’t know anything about namespaces. The class loader does help a lot though, even if there are no namespaces.
Here’s a simple example showing you how to load classes automatically.
All classes you want to load have to specified in an array in your package controller:
$classes = array( 'TestClass' => array('model', 'test_class', $this->pkgHandle), ); Loader::registerAutoload($classes);
If you want to load a helper, replace the first parameter “model” with “helper”. The second parameter is the file name but without the file extension and the last parameter is the handle of the package where our class is located. Once we’ve specified the class names, we can pass it on to Loader::registerAutoload and are almost finished.
When you now want to use our “TestClass”, you can simply use this code:
You can find a working package here https://github.com/Remo/codeblog/tree/master/codeblog_classloader
Once you’ve installed it, you can call the tools file to test everything by opening “/index.php/tools/packages/classloader/test”, e.g. http://localhost/index.php/tools/packages/classloader/test