1

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.

5

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->get('date.formatter'), 
  $container->get('entity.manager')->getStorage('user'), 
  $container->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.

  • Yes i modified like that , now error log says Class 'Drupal\\module_name\\ControllerName\\ClassName' not found and am using it like on top of the file as use \Drupal\module_name\ControllerName\ClassName; and calling it like $instance = ClassName::getinstance($host, $user, $pass); – Crazyrubixfan May 10 '16 at 12:00
  • You are using ControllerName, not Controller. If the class is contained in the module_name/src/Controller directory, the error message is correct. Also, you don't use \\ as separator, but \; so, it is class Drupal\module_name\Controller\ClassName. – kiamlaluno May 10 '16 at 12:04
  • Thanks ! it resolved my problem :) , and thats about error log ,no need to worry – Crazyrubixfan May 10 '16 at 12:08
2

Make sure your class is under my_module\src, in a file called MyClassname.php, then refer to it via its fully qualified namespace:

$instance = Drupal\my_module\MyClassname::getinstance($host, $user, $password);

Or alias it:

// Top of file
use Drupal\my_module\MyClassname;

// Later on
$instance = MyClassname::getinstance($host, $user, $password);
  • many thanks . i was not included the module though :( . – Crazyrubixfan May 10 '16 at 9:57
  • still class not found error coming , i created the module/src/controiller/controller_name.php structure and called the constructor . am using class MyClassName extends ControllerBase instead of a class MyClassName structure – Crazyrubixfan May 10 '16 at 10:40
  • 2
    This isn't Drupal-specific stuff, it's just PHP - you need to learn about Namespaces, autoloading, and how those two things fit together. As far as Drupal knowledge goes, just know that everything in "mymodule\src*" is addressable as Drupal\mymodule\*. So your controller in this case is at Drupal\mymodule\controller\MyClassName. Once you understand namespacing, everything else will make perfect sense. – Clive May 10 '16 at 10:45
  • Yes i have used the name space as use Drupal\module_name\MyClassName; and called it via Drupal\module_name\MyClassName::getinstance($host, $user, $pass); – Crazyrubixfan May 10 '16 at 10:51
  • 2
    Assuming you've followed all the other advice in this answer/comments, that will work fine. Though you don't need to fully qualify the class every time if it's used – Clive May 10 '16 at 10:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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