concrete5 class loader

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),

Complete file:

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:

echo TestClass::saySomething();

Complete code:

You can find a working package here
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

No Comments

You can leave the first : )

Leave a Reply

Your email address will not be published. Required fields are marked *