如何标注在PHP有用吗? [英] How is annotation useful in PHP?

查看:190
本文介绍了如何标注在PHP有用吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何标注在PHP有用吗?我的意思不是一般PHPDoc的

How is annotation useful in PHP? and I don't mean PHPDoc generically.

我只想要一个真实的例子什么的,我想。

I just want a real-world example or something, I guess.

因此​​,根据@最多的回答是:注释完成同样的事情摘要工厂,只能通过专门的PHPDoc的一条线。 - hopeseekr 0秒前修改

So, according to @Max's answer: Annotations accomplish the same thing as Abstract Factories, only via one line of specialized PHPDoc. – hopeseekr 0 secs ago edit

推荐答案

<一个href=\"http://stackoverflow.com/questions/3623499/how-is-annotation-useful-in-php/3623715#3623715\">Rob奥尔莫斯正确的解释是:

基本上注解让你注入行为,并能促进脱钩。

Annotations basically let you inject behavior and can promote decoupling.

在我的话我会说,这些注释是有价值的尤其是在反射的情况下在那里你收集类(附加)元数据/方法/属性您正在检查。

In my words I'd say that these annotations are valuable especially in context of reflection where you gather (additional) metadata about the class/method/property you are inspecting.

另外一个例子,而不是ORM:依赖注入框架。例如即将到来的 FLOW3框架使用docComments /注解,以确定哪些对象在从DI容器创建的,而不是指定一个实例被注入XML配置文件。

Another example instead of ORM: Dependency Injection frameworks. The upcoming FLOW3 framework 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.

以下简化的示例:

您有两个类,一个战士类和武器类。 A 武器实例被在战士实例注入。看看这两个类的定义:

You have two classes, one Soldier class and a Weapon class. A Weapon instance gets injected in a Soldier instance. Look at the definition of the two classes:

class Weapon {
    public function shoot() {
        print "... shooting ...";
    }
}

class Soldier {
    private $weapon;

    public function setWeapon($weapon) {
        $this->weapon = $weapon;
    }

    public function fight() {
        $this->weapon->shoot();
    }
}

如果你会使用这个类,用手注入所有的依赖关系,you'd像这样做:

If you would use this class and inject all dependencies by hand, you´d do it like this:

$weapon = new Weapon();

$soldier = new Soldier();
$soldier->setWeapon($weapon); 
$soldier->fight();

好吧,这是很多样板code的(多多包涵,我来解释一下注解很快是pretty有用)。什么依赖注入框架能为你做的是抽象的创作等组成的物体,并自动注入所有的依赖,你只需做:

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:

$soldier = Container::getInstance('Soldier');
$soldier->fight(); // ! weapon is already injected

右键,但集装箱必须知道哪些依赖一个战士类有。因此,最常见的框架使用XML作为配置格式。示例配置:

Right, but the Container has to know which dependencies a Soldier class has. So, most of the common frameworks use XML as configuration format. Example configuration:

<class name="Soldier">
    <!-- call setWeapon, inject new Weapon instance -->
    <call method="setWeapon">
        <argument name="Weapon" />
    </call>
</class>

但FLOW3使用,而不是XML直接标注在PHP code,以便确定这些依赖关系。在FLOW3,你的战士类是这样的(语法只作为一个例子):

But what FLOW3 uses instead of XML is annotations directly in the PHP code in order to define these dependencies. In FLOW3, your Soldier class would look like this (syntax only as an example):

class Soldier {
    ...

    // ---> this

    /**
     * @inject $weapon Weapon
     */
    public function setWeapon($weapon) {
        $this->weapon = $weapon;
    }

    ...

所以,不需要XML标记战士武器依赖的DI容器。

FLOW 3 AOP 的情况下使用这些注释也,以纪念这应该是编织的方法(装置的方法之前或之后,注入行为)。

FLOW 3 uses these annotations also in the context of AOP, to mark methods which should be "weaved" (means injecting behaviour before or after a method).

至于我个人而言,我不太清楚这些批注的有用性。我不知道,如果它使事情变得更简单,甚至更糟藏这种在PHP code依赖和设置,而不是使用一个单独的文件。

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.

我的工作即G。在Spring.NET中,NHibernate和在PHP中的DI框架(未FLOW3)都基于XML配置文件,并不能说是太困难了。维护这些安装文件也没关系了。

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.

但也许有FLOW3未来的项目证明了对面,注解是去真正的出路。

But maybe a future project with FLOW3 proves the opposite and annotations are the real way to go.

这篇关于如何标注在PHP有用吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆