How to load a class in symfony?

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?

Upvotes: 1

Views: 3246

Answers (2)

macgyver
macgyver

Reputation: 1279

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.

Upvotes: 4

Jeremy Kauffman
Jeremy Kauffman

Reputation: 10413

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?

Upvotes: 2

Related Questions