Calling a public function Over time, I have created some contributed module projects for D7. With D7 EOL not too distant in the future, I need to convert those to D8.

I've just started out doing this, and I've found the OO-style of D8 very different from the procedural style of D7.

For instance, I want to call the public function getTopics() that is part of the Advanced Help project. It can be found inside a file src/AdvancedHelpManager.php, and looks like this:

class AdvancedHelpManager extends DefaultPluginManager {
  …

  /**
   * Search the system for all available help topics.
   * @todo check visibility of the method.
   */
  public function getTopics() {
    $ini = $this-&gt;parseHelp();
    return $ini['topics'];
  }
  …
}


I want to call it from my module, in order to get hold of the list of topics I expect it to return.

So far, I've tried:

$foo = new AdvancedHelpManager();
$bar = $foo-&gt;getTopics();


but this returns the error "Error: Class 'AdvancedHelpManager' not found.

I guess I need to put some sort of "use"-statement on top of the file I try to use it, to make the class visible, but I am currently unable to figure out what that statement should be.
Your current code doesn't work because of namespaces, but even so, that's not the right way to approach your requirement.

AdvancedHelpManager is a plugin manager, which is a service, so you should get the instance of it like so:

$plugin_manager = \Drupal::service('plugin.manager.advanced_help');
$topics = $plugin_manager-&gt;getTopics();


(as always, if you're in a context where DI is available, inject the service instead of calling the static \Drupal method).

If any of this doesn't make sense it would be a very good idea to read the Services and dependency injection docs, which help to explain the architecture behind Drupal 8.