8 – Calling a Method from another Module’s Class

I’m dumb and this is embarrassing, but I’m trying to figure out how I properly use a method from a class in other parts of my code. I need to understand the Drupal way, if not just the php way.

I have a class like this…

namespace Drupalmodule_nameUtilities;

use GuzzleHttpExceptionRequestException;
use DrupalCoreLoggerLoggerChannelFactory;
use DrupalCoreMessengerMessengerInterface;
use SymfonyComponentDependencyInjectionContainerInterface;
use GuzzleHttpClientInterface;
use DrupalCoreConfigConfigFactoryInterface;
use DrupalCoreDependencyInjectionContainerInjectionInterface;

class Example implements ContainerInjectionInterface {

  private $client;
  protected $loggerFactory;
  protected $messenger;
  protected $config;

  public function __construct(
    LoggerChannelFactory $loggerFactory,
    MessengerInterface $messenger,
    ClientInterface $client,
    ConfigFactoryInterface $config
  ) {
    $this->loggerFactory = $loggerFactory->get('simple_mailchimp');
    $this->messenger = $messenger;
    $this->client = $client;
    $this->config = $config->get('module_name.settings');
  }

  public static function create(ContainerInterface $container) {
    return new static(
      $container->get('logger.factory'),
      $container->get('messenger'),
      $container->get('http_client'),
      $container->get('config.factory')
    );
  }

  public function request() {
    return 'whatever';
  }
}

What’s the proper way to use this class somewhere else? For instance, in another module, I want to call the request() method shown above.

  $example = new Example();
  $x = $example->request();

This doesn’t work, as shown above. I get this…

ArgumentCountError: Too few arguments to function Drupalmodule_nameUtilitiesExample::__construct(), 0 passed... and exactly 4 expected

(Even though it seemed to have worked at one time.)

That said, what’s the right way to go about this with Drupal, or in general, I guess?