1

I use symfony 2.4.0. I want to use my custom class as discussed here: Autoloading a class in Symfony 2.1. I have created subfolder in src:

namespace Yur;

class MyClass {
    public go() {
        var_dump('hello!! 32');
    }
}  

In my controller, I made this:

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Yur\MyClass;

class WelcomeController extends Controller
{
    public function indexAction()
    {
        $my = new MyClass();
        $my->go();
        die();
 ...

but it makes an exception:

ClassNotFoundException: Attempted to load class "MyClass" from namespace "Yur" in /var/www/shop.loc/src/Acme/DemoBundle/Controller/WelcomeController.php line 12. Do you need to "use" it from another namespace?

After I have got this exception, I decided consciously to make syntax error exception in my class to see if it loaded. I changed class MyClass ... to class4 MyClass ..., but doesnt got asyntax error` exception. And I decided, that my class is not loaded. Is anyone knows why? And what I must to do to resolve?

1 Answer 1

4

A few things. First, in your code sample above, you have

public go() {
    var_dump('hello!! 32');
}

which should be

public function go() {
    var_dump('hello!! 32');
}

The former raises a parser error in PHP. and probably isn't what you want.

Second, the error

ClassNotFoundException: Attempted to load class "MyClass" from namespace "Yur" in /var/www/shop.loc/src/Acme/DemoBundle/Controller/WelcomeController.php line 12. Do you need to "use" it from another namespace?

is the error Symfony uses when it attempt to autoload a class, but can't find the file. This usually means your file is named incorrectly, or in the wrong folder. I'd tripped check that you have a file in the directory you think you do.

$ ls src/Yur/MyClass.php

You can also add some debugging to the composer autoload code to see what path it's cooking up for your custom class

#File: vendor/composer/ClassLoader.php
public function findFile($class)
{
    //...
    $classPath .= strtr($className, '_', DIRECTORY_SEPARATOR) . '.php';

    //start your debugging code
    if($class == 'Yur\MyClass')
    {
        //dump the generated path
        var_dump($classPath);

        //dump the default include paths
        var_dump($this->fallbackDirs);
    }

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

2 Comments

Thanks! My problem was in that I dont know that klassname and filename must be the same. I made it various. This helps me to determine where problem is: $ ls src/Yur/MyClass.php`
I spent a couple hours not noticing a minor misspelling in a file name, generating this error. Your findFile() tip helped. (I didn't use the sample code, but once I stepped that far into the debugger, I found the issue stepping into this function call within findFile(): $file = $this->findFileWithExtension($class, '.php');

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.