I can't find a clear explanation of how pass parameters to constructor from DI. I have a two classes, every class should be instantiated by passing arguments to his constructor. From documentation I've fonund 2 possiable ways to do it, the first method is clear:
class ProductActivity extends Activity implements IActivity
{
public function __construct($userID)
{
parent::__construct($userID);
}
}
class ActivitiesBuilder
{
/** @var IActivity $activity */
protected $activity;
public function __construct(IActivity $activity)
{
$this->setActivity($activity);
$this->buildActivity();
}
}
$di->set('productActivity', array(
'className' => 'Yapi\Library\Activities\ProductActivity',
'arguments' => array(
array('type' => 'parameter', 'value' => null)
)), true);
$di->set('activityBuilder', array(
'className' => 'Yapi\Library\Activities\ActivitiesBuilder',
'arguments' => array(
array('type' => 'service', 'name' => null),
)), true);
class UserController extends ControllerBase
{
public function couponAction($userID, $limit = 'all')
{
$this->di->getService('productActivity')->setParameter(0, array('type' => 'parameter', 'value' => $userID));
$this->di->getService('activityBuilder')->setParameter(0, array('type' => 'service', 'name' => 'productActivity'));
$this->di->getActivityBuilder()
}
}
This is a second method, what is not clear for me here, is how shold I register the service in this case:
class UserController extends ControllerBase
{
public function couponAction($userID, $limit = 'all')
{
$this->di->get('activityBuilder', array($this->di->get('productActivity', array($userID))));
}
}