You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository has been archived by the owner on Jan 16, 2019. It is now read-only.
Frank Kleine edited this page Apr 7, 2012
·
1 revision
Inversion of Control: Optional injection
Probably you do not want to inject an object every time, because the class will work fine without the dependency. It is possible to mark an injection as optional:
#php
<?php
class BMWWithCoDriver extends BMW {
protected $codriver;
/**
***@Inject(optional=true)
***/
public function setCoDriver(CoDriver $codriver) {
$this->codriver = $codriver;
}
public function moveForward($miles) {
if (null !== $this->codriver) {
$this->codriver->sayHello();
}
parent::moveForward($miles);
}
}
?>
If the injection would not be marked as optional and no binding would be defined for CoDriver retrieving the instance of BMWWithCoDriver would result in a stubBindingException. But by marking the injection as optional there will be no exception thrown and the instance of BMWWithCoDriver will be created without setting the codriver property.