-
Notifications
You must be signed in to change notification settings - Fork 2
Annotations
jamolkhon edited this page Mar 11, 2012
·
4 revisions
Each annotation is a class that implements Sharbat\Inject\Annotation
interface. All Sharbat annotations are namespaced and written inside phpDoc blocks in the following manner:
/**
* \Sharbat\@Scope(\Sharbat\Inject\Singleton)
* or simply
* \Sharbat\@Singleton
*/
class ASingletonClass {
/**
* \Sharbat\@Inject(ADependencyClass)
*/
private $aDependencyClass;
private $anotherDependency;
/**
* \Sharbat\@Inject
*/
public function setAnotherDependency(AnotherDependency $anotherDependency) {
$this->anotherDependency = $anotherDependency;
}
}
Long-written (fully qualified) annotations are not very readable nor fun. One would want to write them like @Inject
and @Singleton
. This is definitely doable. For each annotation create a class (preferably with the same name) in the global namespace, that extends the target annotation class.
class Inject extends \Sharbat\Inject {}
class Singleton extends \Sharbat\Singleton {}
Now the following will work as expected:
/**
* @Singleton
*/
class ASingletonClass {
/**
* @Inject(ADependencyClass)
*/
private $aDependencyClass;
private $anotherDependency;
/**
* @Inject
*/
public function setAnotherDependency(AnotherDependency $anotherDependency) {
$this->anotherDependency = $anotherDependency;
}
}