D_Breedt D_Breedt - 1 year ago 143
PHP Question

PHP symfony error: "did you forget to use a use statement", regardless of class/function

I have started building a new Web App using Symfony, but am having issues using classes.

PhpStorm is able to find the functions within the classes (due to the fact that it gives suggestions when you type "$className->".

Also, to prove this is not the same as the other similar questions, I have over simplified it, and even so, the error still occurs.

I have the below in my DefaultController:

<?php

namespace AppBundle\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\Request;
use testBundle;
use AppBundle\domainionClasses\firstlevelchecks;

class DefaultController extends Controller
{
/**
* @Route("/register",name="register")
*/
public function registerAction(){
$testClass = new firstlevelchecks();
$testing = $testClass->donothing(); //the IDE knows that the function donothing() exists, in fact it suggests it.
return new Response('');
}
}


The below is the php class located in AppBundle/domainionClasses/test1.php

<?php

namespace AppBundle\domainionClasses;


class firstlevelchecks
{
function donothing(){
return null;
}
}


When loading the /register route, the below Symfony error is displayed:

Attempted to load class "firstlevelchecks" from namespace "AppBundle\domainionClasses".
Did you forget a "use" statement for another namespace?

It is Attempting to load the class from the correct name space, and I have used a use statement.

Is there anything I am missing here, or is there a problem with Symfony? This is the first time I used the new version of PhpStorm, and have just downloaded the plugin, also the first time I have experienced this issue :(

Answer Source

Because your file is named test1.php, how can the autoloader know which file to include ?

You shoud rename it to firstlevelchecks.php (= the name and case of your class).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download