I am loading a custom class from the lib folder and I'm getting the following error:
Fatal error: Class 'NameSpace_MyClass' not found in /dir/to/site/app/code/local/NameSpace/Module/controllers/Adminhtml/ModuleController.php on line 93
My class directory tree looks like this
./lib
/NameSpace
/MyClass.php
/Varian
/Zend
The declaration in the controller looks like:
$class = new NameSpace_MyClass($id);
And MyClass.php
looks like this
class MyClass{
private $id;
public function __construct($id) {
$this->id = $id;
}
}
How am I able to call this class? I'm under the impression I don't need to do anything in an xml file to load this as Magento/Zend auto includes files in the lib
folder. Is this correct?
Best Answer
For the Magento autoloader to find it, the class must be called
NameSpace_MyClass
, notMyClass
and also notNameSpace\MyClass
If you want to use libraries with real namespaces, you need an additional autoloader that is PSR-0 or PSR-4 compatible. I can recommend the Magento-PSR-0-Autoloader
With this extension, you would register the namespace in
config.xml
like this:Then, define a real namespace:
And use to the class like this: