[TYPO3-mvc] How should/can I pass parameters to an object's constructor using dependency injection?

Simon Tuck stu at rtpartner.ch
Fri Nov 19 15:28:56 CET 2010


On 18.11.10 17:22 , Bastian Waidelich wrote:
> But you can't inject simple types (string, integer, boolean, ...)
> because.. How should the DI container know what value to inject?
>
> This works:
>
> /**
> * @param Tx_YourExt_SomeClass $someObject
> */
> public function __construct(Tx_YourExt_SomeClass $someObject) {
> $this->setSomeObject($someObject);
> }
>

Hi Bastian,
Hmmm... Sorry, I still don't get it. Perhaps you could explain the 
following example:

class Tx_MyExt_ThisClass
     extends Tx_Extbase_DomainObject_AbstractValueObject
{
     public function injectThatClass(Tx_MyExt_ThatClass $thatClass)
     {
         $this->thatClass = $thatClass;
     }
}

class Tx_MyExt_ThatClass
     extends Tx_Extbase_DomainObject_AbstractValueObject
     implements t3lib_Singleton
{
     public function __construct(Tx_YourExt_SomeClass $someObject)
     {
         $this->setSomeObject($someObject);
     }
}

As you can see I have a class "ThisClass" which needs to inject 
"ThatClass", but ThatClass's constructor takes an argument. If ThatClass 
hasn't been instantiated how will the the DI container be able to 
instantiate ThatClass without being passed it's constructor parameter? 
Are you saying the DI container will automatically "get" the constructor 
parameter for ThatClass and that I cannot pass the parameter for 
ThatClass's constructor to the DI container?

Thanks!
Simon





More information about the TYPO3-project-typo3v4mvc mailing list