4

Within my extension I can load classes from CiviCRM's core codebase like this:

$foo = new \Civi\Core\AssetBuilder();

And I can also load custom classes that I've written in my extension like this:

$foo = new CRM_MyExtension_MyClass();

Cool.


But if I've defined a custom class within my extension using the Civi namespace, the following does not work:

$foo = new \Civi\MyExtension\MyClass();

It fails with this error message:

Error: Class 'Civi\MyExtension\MyClass' not found in ...

How can I load this custom class from within my extension?

6

Add the following elements to your extension's info.xml file to enable PSR-4 autoloading:

<classloader>
  <psr4 prefix="Civi\" path="Civi"/>
</classloader>

Also see docs on the <classloader> element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.