-- Ralf Eggert <[email protected]> wrote (on Thursday, 16 August 2012, 08:43 PM +0200): > Hi again, > > another question came to my mind. Which way do you prefer. > > Using the constructor to pass objects: > > 'User\Mapper\User' => function($sm) { > $table = $sm->get('User\Table\User'); > $mapper = new UserMapper($table); > return $mapper; > }, > > Or using setter methods to pass objects > > 'User\Mapper\User' => function($sm) { > $table = $sm->get('User\Table\User'); > $mapper = new UserMapper(); > $mapper->setDbTable($table); > return $mapper; > }, > > Any comments?
Constructor arguments are for required collaborators -- stuff that, if it's missing, will mean no operations you do in the object will work. Additionally, I typically only like constructor arguments if changing the collaborator will cause problems later. Setter injection is for either collaborators that are optional (i.e., some or all operations can work without them), or which you allow replacing at any point during operations without side-effects. -- Matthew Weier O'Phinney Project Lead | [email protected] Zend Framework | http://framework.zend.com/ PGP key: http://framework.zend.com/zf-matthew-pgp-key.asc -- List: [email protected] Info: http://framework.zend.com/archives Unsubscribe: [email protected]
