Skip to content

Annotations

jamolkhon edited this page Feb 21, 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\Inject\@InScope(\Sharbat\Inject\Singleton)
 */
class ASingletonClass {

  /**
   * \Sharbat\Inject\@Inject(ADependencyClass)
   */
  private $aDependencyClass;
  
  private $anotherDependency;
  
  /**
   * \Sharbat\Inject\@Inject
   */
  public function setAnotherDependency(AnotherDependency $anotherDependency) {
    $this->anotherDependency = $anotherDependency;
  }

}

Typing fully qualified name every time is not 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\Inject {}

class Singleton extends \Sharbat\Inject\InScope {
  public function __construct() {
    parent::__construct('\Sharbat\Inject\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;
  }

}
Clone this wiki locally