Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="https://stackoverflow.com/questions/3623499/how-is-annotation-useful-in-php/3623715#3623715">Rob Olmos</a> explained it right: </p> <blockquote> <p>Annotations basically let you inject behavior and can promote decoupling.</p> </blockquote> <p>In my words I'd say that these annotations are valuable especially in context of <a href="http://php.net/reflection" rel="noreferrer">reflection</a> where you gather (additional) metadata about the class/method/property you are inspecting.</p> <p>Another example instead of ORM: <a href="http://en.wikipedia.org/wiki/Dependency_injection" rel="noreferrer">Dependency Injection</a> frameworks. The upcoming <a href="http://flow3.typo3.org/" rel="noreferrer">FLOW3 framework</a> for example uses docComments/annotations to identify which objects are injected in an instance created from a DI container instead of specifying it in an XML configuration file. </p> <p>Oversimplified example following:</p> <p>You have two classes, one <code>Soldier</code> class and a <code>Weapon</code> class. A <code>Weapon</code> instance gets injected in a <code>Soldier</code> instance. Look at the definition of the two classes:</p> <pre><code>class Weapon { public function shoot() { print "... shooting ..."; } } class Soldier { private $weapon; public function setWeapon($weapon) { $this-&gt;weapon = $weapon; } public function fight() { $this-&gt;weapon-&gt;shoot(); } } </code></pre> <p>If you would use this class and inject all dependencies by hand, you´d do it like this:</p> <pre><code>$weapon = new Weapon(); $soldier = new Soldier(); $soldier-&gt;setWeapon($weapon); $soldier-&gt;fight(); </code></pre> <p>All right, that was a lot of boilerplate code (bear with me, I am coming to explain what annotations are useful for pretty soon). What Dependency Injection frameworks can do for you is to abstract the creation such composed objects and inject all dependencies automatically, you simply do:</p> <pre><code>$soldier = Container::getInstance('Soldier'); $soldier-&gt;fight(); // ! weapon is already injected </code></pre> <p>Right, but the <code>Container</code> has to know which dependencies a <code>Soldier</code> class has. So, most of the common frameworks use XML as configuration format. Example configuration:</p> <pre><code>&lt;class name="Soldier"&gt; &lt;!-- call setWeapon, inject new Weapon instance --&gt; &lt;call method="setWeapon"&gt; &lt;argument name="Weapon" /&gt; &lt;/call&gt; &lt;/class&gt; </code></pre> <p>But what FLOW3 uses instead of XML is annotations directly in the PHP code in order to define these dependencies. In FLOW3, your <code>Soldier</code> class would look like this (syntax only as an example):</p> <pre><code>class Soldier { ... // ---&gt; this /** * @inject $weapon Weapon */ public function setWeapon($weapon) { $this-&gt;weapon = $weapon; } ... </code></pre> <p>So, no XML required to mark the dependency of <code>Soldier</code> to <code>Weapon</code> for the DI container. </p> <p>FLOW 3 uses these annotations also in the context of <a href="http://en.wikipedia.org/wiki/Aspect-oriented_programming" rel="noreferrer">AOP</a>, to mark methods which should be "weaved" (means injecting behaviour before or after a method). </p> <hr /> <p>As far as I am concerned, I am not too sure about the usefulness about these annotations. I dont know if it makes things easier or worse "hiding" this kind of dependencies and setup in PHP code instead of using a separate file.</p> <p>I worked e. g. in Spring.NET, NHibernate and with a DI framework (not FLOW3) in PHP both based on XML configuration files and cant say it was too difficult. Maintaining these setup files was ok, too. </p> <p>But maybe a future project with FLOW3 proves the opposite and annotations are the real way to go. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload