0

Can anyone recommend a way to troubleshoot why a particular class isn't being loaded by the autoloader? I'm using namespace autoloading as such:

require_once 'Zend/Loader/Autoloader.php';
$loader = Zend_Loader_Autoloader::getInstance();
$loader->registerNamespace('\Xyz');

if (APPLICATION_ENV == 'development') {
    $loader->suppressNotFoundWarnings(false);
}

Some classes in the Xyz namespace are being loaded while others aren't. In the place where the error is being thrown about the class not being found, I can add a require_once to the class file and it works fine.

I've seen this question: zend_loader_autoloader does not seem to load abstract class where the poster debugged Zend_Loader, but when I do so, the only classes I see it autoloading are Zend_ and ZendX_ classes. Where do other registered namespaces get loaded, and is there a way to dump all of the classes that are loaded at a given point in my code?

Community
  • 1
  • 1
Luke
  • 768
  • 2
  • 14
  • 33
  • I'm not really sure so I post this as a comment but in the namespace you should use the '_' and not the '\'. – Aurelio De Rosa Oct 13 '11 at 20:39
  • Thanks Aurelio, but according to this page, as of ZF 1.10, true PHP namespace autoloading is supported: http://framework.zend.com/manual/en/zend.loader.autoloader.html See the "Note: Loading Classes from PHP Namespaces" section. Also, some of my classes are successfully loading this way. I just need to figure out why some other classes are not. – Luke Oct 13 '11 at 20:42
  • 1
    Some random 'maybe' problems: 1) remove the leading \. As far as I know it is not required. 2) Namespace/Class does not match the directory structure. 3) Add a leading \ when referring to the object: 'new \My\Test();' Debugging the autoloader is quite difficult as all autoloaders relay on the php-function which isn't that talky. – Fge Oct 13 '11 at 20:50
  • Fge, I've tried without the leading slash in the autoloader namespace and it doesn't seem to work. I get errors because it's trying to load the class relative to the current namespace where the class is instantiated. I've checked the directory structure/class name multiple times, but can't see any discrepancy in the structure vs. namespace. I tried adding the full namespace (including leading \) to the beginning of the class name when I'm instantiating it, but I get the same class not found error. In this case, again, if I add the `require_once` it works fine. I'm stumped. – Luke Oct 13 '11 at 21:04
  • I take it you have already tried `$loader->registerNamespace('Xyz_'); ` – vascowhite Oct 13 '11 at 22:30
  • I had this problem as well. Solved this by adding a leading \ before classes that are loaded from an other namespace (indicating I'm going from the "root namespace"): new \My\Test(); – Fge Oct 13 '11 at 23:07
  • @vascowhite, the underscore isn't necessary unless you are using userland namepacing where underscores in class names are used as namespace delimiters. – Luke Oct 14 '11 at 19:36
  • @Fge Thanks for the advice. I did end up removing the leading slash in my `$loader->registerNamespace('\Xyz');`. – Luke Oct 14 '11 at 19:36

2 Answers2

0

You need to specify to application.ini

1) autoloaderNamespaces[] = "Xyz"

2) Be shore that you have the Xyz folder in your library .

3) Create a file like test.php in Xyz

4) In test.php you need to have class Xyz_test extends ....

5) In index controler try to $var = new Xzy_test();

axel22
  • 32,045
  • 9
  • 125
  • 137
razvancod
  • 21
  • 1
  • Thanks for the reply; however, specifying autoloaderNamespaces[] in the application.ini I believe (correct me if I'm wrong) is just another way of doing the same thing I'm doing with `$loader->registerNamespace();`. Also, when using true PHP namespacing, rather than "userland" namespacing (Xyz_My_Class), it is not required to name your classes with underscores. In other words, I'd have a directory structure and namespace of Xyz\My and a file path of Xyz\My\ClassName.php. Then I can instantiate it as `new ClassName()` as long as I've imported the namespace in the file I'm instantiating it in. – Luke Oct 14 '11 at 19:27
0

I ended up figuring out what my problem was. I was finally able to track it down by stepping through the autoloader line by line. What happened was when I was converting my classes from using "userland" namespaces (Xyz_My_ClassName) to use a PHP namespace definition at the top of each class, I missed changing my import declarations, so the autoloader was looking for the old class name. Thanks to everyone who helped me track this down.

Luke
  • 768
  • 2
  • 14
  • 33