0

I have created a module in drupal 8 . I need to use a class which is in external file .

File name : drupal.php

Class name : class newman{...}

In drupal.php ( have many classes ):

<?php
namespace Drupal\mymodulename\lib;
class newman{
   public function new(){
       $dp = 'TEST';
       return $dp;
   }
}
?>

In my controller :

<?php
namespace Drupal\mymodulename\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\mymodulename\lib\drupal;

class mmController extends ControllerBase {
    public function man() {
          $this->man=new newman();
          $build['test'] = [
                            '#markup' => $man->new(),
                           ];
    }
}

Error: Class 'Drupal\mymodulename\Controller\newman' not found in Drupal...

How can I fix it ?

4
  • Avoid using a class name of drupal / filename drupal.php.
    – Kevin
    Apr 7, 2017 at 23:00
  • This is still another PHP question about namespaces, not a Drupal question. You first define the namespace to be Drupal\mymodulename\lib, then the code contains use Drupal\mymodulename\lib\drupal; which is telling PHP to use the Drupal\mymodulename\lib\drupal class, which is not the class you first defined. And since you aren't using a use statement for the newman class, PHP doesn't know what class you are trying to use.
    – apaderno
    Apr 10, 2017 at 11:31
  • To make it clearer: Drupal question doesn't mean every question I have while using Drupal or developing a Drupal module. A plain PHP question that doesn't require any Drupal knowledge to be answered is not a Drupal-specific question, and it is off-topic for us. We don't teach PHP, including how PHP uses namespaces.
    – apaderno
    Apr 10, 2017 at 11:34
  • Also, basing on the comments left for the answer, you aren't even showing the real code you are using. It is quite hard to help when you aren't showing the code you use.
    – apaderno
    Dec 26, 2017 at 9:07

1 Answer 1

3

Your use statement is wrong. It should be:

use Drupal\mymodulename\lib\newman; - no Drupal on the end.

Aside from that, you should strive to inject dependencies in the constructor.

Also, there should not be a difference between the file name and classname. It makes it harder to debug and organize. The file should be named newman.php not drupal.php.

The error is referencing the fact that it cannot locate your class at the given namespace. It would help to brush up on PSR-4/autoloading in Drupal 8 as well as naming conventions and standards.

4
  • In real I use nusoap.php file and this file has many class . I want use nusoap_client class in nusoap.php . how can I do this ?
    – user60258
    Apr 8, 2017 at 6:25
  • What does this have to do with NuSoap?
    – Kevin
    Apr 8, 2017 at 12:53
  • Payment method need this file.
    – user60258
    Apr 8, 2017 at 12:56
  • @Kevin The OP is not showing the code he is effectively using. I take he is trying to use nusoap.php, which doesn't support namespaces, and he is not able to convert the classes in classes using namespaces. Your answer is correct for what the OP shows, but what the OP shows is not the code he is trying to use.
    – apaderno
    Dec 26, 2017 at 9:09