3

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?

4
  • 1
    Add require_once Mage::getBaseDir('base').DS.'lib'.DS.'Namespace'.DS.'MyClass.php'; in your controller file Commented Jul 1, 2015 at 5:06
  • Hi, @saravanavelu I have added that into the _constructor of MyController behind a if(!class_exists('NameSpace_MyClass')) and it throws up this error Fatal error: Cannot redeclare class MyClass in the class file where it declares the class name
    – Joshua
    Commented Jul 1, 2015 at 5:37
  • @saravanavelu likewise if I call the same code in just the controller action
    – Joshua
    Commented Jul 1, 2015 at 5:39
  • @saravanavelu please submit your comment as an answer and I'll accept it. My problem is now different and i'll create a new question.
    – Joshua
    Commented Jul 1, 2015 at 5:52

2 Answers 2

3

For the Magento autoloader to find it, the class must be called NameSpace_MyClass, not MyClass and also not NameSpace\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:

<psr0_namespaces>
    <NameSpace />
</psr0_namespaces>

Then, define a real namespace:

namespace NameSpace;

class MyClass{

    private $id;

    public function __construct($id) {
        $this->id = $id;
    }
}

And use to the class like this:

use NameSpace\MyClass;
$class = new MyClass($id);
4
  • Thank you so much. Changed the class name and it was perfect.
    – Joshua
    Commented Jul 1, 2015 at 6:36
  • Hi,Fabian, I am using this monolog module github.com/aleron75/magemonolog, on local it is working fine, but on server it throws "Fatal error: Class 'Monolog\Logger' not found in" is this somehow related to
    – Haris
    Commented Apr 10, 2018 at 21:04
  • on local, php version is 5.6, on server it is 5.5.38
    – Haris
    Commented Apr 10, 2018 at 21:05
  • @Haris no, it must be something else that's different between the two installations Commented Apr 11, 2018 at 7:45
0

You have *nix system and the file system is case senstive. This means your directories has to be the same name, EXACT same name, as the file:

     NameSpace_MyClass
/lib/Namespace/MyClass.php

What can you see? If you haven't seen it yet you need to increase this ability, it will help you a lot with Magento ;-) ... S != s

1
  • Hi, The casing error was a transposing error, the directory is called NameSpace on live.
    – Joshua
    Commented Jul 1, 2015 at 6:14

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.