1

I need to load a class in Symfony 1.4 but the simple placing of the file in /apps/frontend/lib seems not to be enough.

class test
{ function foo ($foo) { echo $foo; }
}

and I tried to use it in an action:

  public function executeTest(sfWebRequest $request)
  {
    $a = new test();
    $a->foo('aaa');
  }  

I have refreshed the cache and still:

Fatal error: Class 'test' not found in ...

Do I have to declare it somewhere?

2 Answers 2

4

You have to place your file in your project lib folder:

myproject/lib

[Updating] About loading a class from a file in myproject/apps/frontend/lib/ maybe the problem is the filename where is the class. I found in this page A Gentle Introduction to symfony - Chapter 2 - Exploring Symfony's Code that:

Symfony will then look for a MyClass definition in all files ending with class.php in one of the project's lib/ directories. If the class definition is found, it will be included automatically.

So if you store all your classes in lib/ directories, you don't need to include classes anymore. That's why symfony projects usually do not contain any include_once or require_once statements.

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

2 Comments

Yup. See also my brief explanation of how the Symfony autoloader searches for classes in this earlier answer.
Thanks! the problem was at the name.
2

Contrary to the other answer, classes in /apps/frontend/lib/ absolutely should be loaded by the autoloader if you are in a frontend action.

Something else is going on here. Is your class actually named test? If you're in production, have you cleared your cache?

1 Comment

In my first answer I tried to provide a quick solution, and certainly not have been accurate. Thanks for making me think.

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.