Martin Melka
Martin Melka

Reputation: 7799

Attempted to load class "ClassName" from namespace (...). Even though namespace is imported

I have a Symfony project to which I added some non-symfony php files containing various classes. But for some reason the classes are not loaded when loading the website, even though the IDE sees them properly.

So, I have a class that needs other classes:

namespace rootspace\FrontBundle\Controller;

use rootspace\FrontBundle\Networks\TwitterOAuth;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template;

class TwitterController extends Controller
{
    public function connectAction(){
        // The TwitterOAuth instance
        $connection = new TwitterOAuth('abc', '123');
    }
}

And then the class which fails to load (that needs yet another file)

namespace rootspace\FrontBundle\Networks;

/* Load OAuth lib. You can find it at http://oauth.net */
//require_once('OAuth.php'); -- should this be commented out?

/**
 * Twitter OAuth class
 */
class TwitterOAuth {
  /* Contains the last HTTP status code returned. */
}

Lastly, the third file

namespace rootspace\FrontBundle\Networks;
use Symfony\Component\Config\Definition\Exception\Exception;

class OAuthConsumer
{
    public $key;
    public $secret;
}
(...)

I assume the actual filenames don't matter, right? Nor their structure? PhpStorm sees all the classes properly, I can right-click through them, but it fails when deployed.

Thanks for help

Edit - the whole error message says

Attempted to load class "TwitterOAuth" from namespace "rootspace\FrontBundle\Networks" in D:\Dropbox\project\src\rootspace\FrontBundle\Controller\TwitterController.php line 15. Do you need to "use" it from another namespace?

Upvotes: 3

Views: 10114

Answers (2)

Winnipass
Winnipass

Reputation: 950

I forgot to add a .php extension to my filename

Upvotes: 0

Tomasz Madeyski
Tomasz Madeyski

Reputation: 10900

This is because Symfony's autoloader follows PSR standards (PSR-0, PSR-4) which says that fully qualified (with namespace) class name translates to file location and name. So in fact file names does matter. So in your case rootspace\FrontBundle\Networks\TwitterOAuth class should be located in rootspace/FrontBundle/Networks directory in file called TwitterOAuth.php

If classes you are using does not follow PSR standards you can also register them manually in app/autoloader.php file

Check these for more info:

How can I add a namespace to Symfony 2.1?:

How to autoload class

And check this answer

Upvotes: 7

Related Questions