PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Saturday, March 5, 2022

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

 March 05, 2022     namespaces, php, symfony     No comments   

Issue

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?

Solution

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



Answered By - Tomasz Madeyski
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing