1

I'm a little new to the concept of Namespaces in PHP/Drupal, and I'm trying to figure out where I am going wrong here.

I've got this custom PHP library under the /libraries folder - let's call it ilib. And inside of ilib, there is another directory called lms.

Anyway, I've written a little module that will connect with ilib (or I am attempting to get it to connect with it), called ilib_connect.

I am trying to do a test function inside a custom page with a controller for the page, as such:

namespace Drupal\ilib_connect\Controller;

use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Link;
use Drupal\Core\Url;
use Symfony\Component\HttpKernel\Exception\AccessDeniedHttpException;
use ilib\lms;

/**
 * Controller routines for page example routes.
 */
class iLibController extends ControllerBase {

function ilib_connect_test() {
  $lms = new \ilib\lms;
  \Drupal::logger("ilib_connect")->notice("<pre>" . $lms . "</pre>");
  $lms->AddLMSActivity();
  return "fake markup";
  }

}

However I am getting the error:

Error: Class 'ilib\lms' not found in Drupal\ilib_connect\Controller\iLibController->ilib_connect_test() (line 17 of modules/custom/ilib_connect/src/Controller/iLibController.php).

So I'm sorta having trouble pulling any classes from ilib into scope.

How exactly do I pull a third party library into scope in a situation like this? What about in a .module file?

What exactly am I misunderstanding here?

EDIT: Oh and just to be clear, this is how I am registering the custom library with Composer:

"autoload": {
"psr-4": {"ilib\\": "web/libraries/ilib"}
}

EDIT 2 - question to Clive:

Ah, so say I've got a file named AddLMSActivity.php inside of libraries/ilib/lms, setup like this:

namespace ilib\lms;

class AddLMSActivity
{

}

How would I access it exactly?

ilib\lms\AddLMSActivity?

4
  • 1
    inside of ilib, there is another directory called lms and use ilib\lms; are incompatible. You can't use a directory, just a class, function, trait or const
    – Clive
    Commented Dec 22, 2016 at 14:27
  • Ah, so say I've got a file named AddLMSActivity.php inside of libraries/ilib/lms, setup like this: namespace ilib\lms; class AddLMSActivity { } How would I access it exactly? ilib\lms\AddLMSActivity? Added the question in an edit so that the formatting is better.
    – Jack Ryan
    Commented Dec 22, 2016 at 15:17
  • Cool, that does seem to be the case. I figured out everything I needed from this comment. If you want to make it into an answer I'll accept it.
    – Jack Ryan
    Commented Dec 22, 2016 at 15:32
  • Looks like it been taken care of @Andrew
    – Clive
    Commented Dec 22, 2016 at 17:09

1 Answer 1

1

When you use use you need to provide namespace + classname. This is what you are doing already, so

use ilib\lms;

Should be replaced with

use ilib\lms\AddLMSActivity;

If you want to use the AddLMSActivity class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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