I want to work with Interfaces and Dependency Injection in my Yii 1.1.14 project. Here is the demo code that I have:
The interface:
interface IUserInterface
{
public function DoSomething();
}
The class:
class UserService implements IUserInterface
{
public function DoSomething()
{
echo "TEST TEST";
}
}
Now comes the part that is problematic for me. How do I inject the interface in my controller?
I have tried this:
class AccountController extends Controller
{
protected $userService;
public function __construct(IUserInterface $userInterface)
{
$this->userService = $userInterface;
parent::__construct();
}
public function actionTest()
{
$this->userService->DoSomething();
}
}
but this won't work, because of CController constructor:
public void __construct(string $id, CWebModule $module=NULL)
What should I do, so I can use the interface in my controller?
I asked the same question on the Yii forum, but we ended up going in circles: http://www.yiiframework.com/forum/index.php/topic/52810-using-interfaces-and-di-in-yii-controllers/