3

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?
3
  • Do you use composer? Commented Jan 30, 2015 at 1:00
  • 1
    For autoloading, filenames need to match class names. Can you post your error message? Commented Jan 30, 2015 at 1:58
  • Yeah, I use composer. But I did not use it for creation of the classes. Should I? I am new to PHP :| So does this mean that I need to have only one class in one file? The TwitterOAuth class is in a file of the same name, but it contains more classes. The OAuthConsumer is in file OAuth.php. Commented Jan 30, 2015 at 3:20

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

Comments

0

I forgot to add a .php extension to my filename

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.