3

I have strange problem or maybe I'm doing something wrong. I've create my own module. My .info file looks like this:

...
files[] = DdFactory.inc
files[] = ArticleManager.inc
...

In ArticleManager.inc I have class ArticleManager (no namespace). Than I would like to use DFactory (namespace Dd) class in ArticleManager.

Here is the beginning of DFactory.inc:

<?php

namespace Dd;

class Factory
{
...

In ArticleManger I have:

$ob = new \Dd\Factory();

And error:

Fatal error:  Class 'Dd\Factory' not found

I can make it work in two ways. Or I have to remove namespaces than $ob = new Factory(); or I have to add include 'DFactory.inc' at top of ArticleManager.inc

Could somebody tells me how to run it with namespaces and without the include? I'm using drupal 7.

2 Answers 2

3

Try the xautoload (http://drupal.org/project/xautoload) module to help with autoloading. It's faster and less fragile then the D7 core code registry.

XAutoload implements the PSR-0 standard, which is how Drupal 8 will handle namespaces, so it's the most future proof way.

0
1

The code registry does not support namespaces, so far. I tried adding the following code to the classes.php a module of mine declares in its .info file.

class codetesting {
  protected $file;
}

namespace code;

class testing {
  protected $file;
}

Looking at the "registry" table, I noticed the namespace is not used. This means that when I try to create an instance of \code\testing, Drupal does not find the file containing its definition, as the registry contains information for the testing class.

What you can do is altering the registry table to include the namespace in the "name" column.

Your Answer

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

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