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 foun开发者_如何学Pythond in ...
Do I have to declare it somewhere?
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.
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?
精彩评论