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();


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?

1 Answer 1


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

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

Also see docs on the <classloader> element

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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