1

I've got a class defined with:

namespace Drupal\mymodule\Component

class MyCustomClass {
  public static $aVar;

  public static function do_a_thing(){
    return self::$aVar;
  }
}

And in mytheme.theme I've got:

use Drupal\mymodule\Component\MyCustomClass;

function mytheme_preprocess_paragraph(&$variables) {
  switch($paragraph->getType()) {
    case 'paragraph_name':
      $thing = MyCustomClass::do_a_thing();
  }
}

This is throwing Error: Class 'Drupal\mymodule\Component\MyCustomClass' not found in mytheme_preprocess_paragraph(). What gives? The module is enabled and my theme is active/default. I've tried rebuilding cache and restarting the server.

closed as off-topic by kiamlaluno Jan 8 at 8:56

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "This question was caused by a problem that can no longer be reproduced, was solved by a cache clear, or was a simple typographical error. While similar questions may be on-topic here, this one was resolved in a manner unlikely to help future readers." – kiamlaluno
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Go over to phptherightway.com for a quick update and how the PSR-4 autoloader works and it will illuminate you on all of this much faster. Also some of the initial Symfony docs too. – Kevin Jan 8 at 0:45
1

Is the file name/path right? It should be mymodule/src/Component/MyCustomClass.php – Clive

Multiple classes in one file is bad practice, and at the same time, a class file should be named the class that it contains. There’s nothing to gain from putting multiple classes in one file. – Kevin

As noted by @Clive and @Kevin in the comments to my question, I needed to fix the name of the file containing my class to MyCustomClass.php.

I didn't realize that the two needed to share the same name

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