Calling a class defined from another module I am calling the method defined in a class implemented from another module using the following code.

use Drupal\module_name\MyClassName;

$instance = Drupal\module_name\MyClassName::getinstance($host, $user, $pass);


My code doesn't work, as I get an error about a class not found. What am I doing wrong?

The class is contained in module/src/controiller/controller_name.php.
In your comment, you say the class is inside module/src/controiller/controller_name.php, but you refer to the class as Drupal\module_name\MyClassName. That is wrong.
Assuming module_name is the directory containing your module (and the short name of the module itself), and that module_name/src/Controller/controller_name.php is the file containing the class definition, you need to use the following code.

use \Drupal\module_name\Controller\MyClassName;

$instance = MyClassName::getinstance($host, $user, $pass);


That is evident in any file referring a class, for example user.routing.yml.

user.logout:
  path: '/user/logout'
  defaults:
    _controller: '\Drupal\user\Controller\UserController::logout'
  requirements:
    _user_is_logged_in: 'TRUE'


As you notice, the class is referenced as \Drupal\user\Controller\UserController, not \Drupal\user\UserController.

Also, the filename is generally the name of the class plus the php extension, so in your case the file name should be module_name/src/Controller/MyClassName.php, not module_name/src/Controller/controller_name.php.

As side note, if you are calling a static method for a controller class, you probably need to do something more than just invoking it. Normally, controllers get also the dependency injection container; without that, they would not work. See for example UserController::create().

public static function create(ContainerInterface $container) {
  return new static(
  $container-&gt;get('date.formatter'), 
  $container-&gt;get('entity.manager')-&gt;getStorage('user'), 
  $container-&gt;get('user.data')
  );
}


If you call controller a class that is not a controller class as Drupal defines that, then your code could work; differently, if you are calling a Drupal controller class, resolved the error about a class not found, you will get more errors to fix.