Answers.Body: <p>Add the following elements to your extension's <code>info.xml</code> file to enable <a href="http://www.php-fig.org/psr/psr-4/" rel="noreferrer">PSR-4 autoloading</a>:</p>
  
  <pre><code>&lt;classloader&gt;
    &lt;psr4 prefix="Civi\" path="Civi"/&gt;
  &lt;/classloader&gt;
  </code></pre>
  
  <p>Also see docs on the <a href="https://docs.civicrm.org/dev/en/latest/extensions/info-xml/#classloader" rel="noreferrer"><code>&lt;classloader&gt;</code> element</a></p>
  
Answers.ContentLicense: CC BY-SA 3.0
Answers.CreationDate: 2017-09-07T05:06:43.443Z
Answers.LastActivityDate: 2017-09-07T05:06:43.443Z
Answers.UserDisplayName: Sean
Body: <p>Within my extension I can load classes from CiviCRM's core codebase like this:</p>
  
  <pre><code>$foo = new \Civi\Core\AssetBuilder();
  </code></pre>
  
  <p>And I can also load custom classes that I've written in my extension like this: </p>
  
  <pre><code>$foo = new CRM_MyExtension_MyClass();
  </code></pre>
  
  <p>Cool.</p>
  
  <hr>
  
  <p>But if I've defined a custom class within my extension using the <code>Civi</code> namespace, the following does not work:</p>
  
  <pre><code>$foo = new \Civi\MyExtension\MyClass();
  </code></pre>
  
  <p>It fails with this error message:</p>
  
  <blockquote>
    <p>Error: Class 'Civi\MyExtension\MyClass' not found in ...</p>
  </blockquote>
  
  <p>How can I load this custom class from within my extension?</p>
  
Tags: <developer>
Title: Within an extension how can load a custom `Civi` name-spaced class?
ThreadText: # Within an extension how can load a custom `Civi` name-spaced class?
  
  (Asked by: username_0 on 2017-09-07)
  
  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?
  
  **Tags:** developer
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2017-09-07. Score: 7
  
  Add the following elements to your extension's info.xml file to enable PSR-4 autoloading:
  
    
  
  
  Also see docs on the  element
  
  
  ---
  
  