注释在 PHP 中有什么用? [英] How is annotation useful in PHP?

查看:29
本文介绍了注释在 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.

因此,根据@Max 的回答:注释与抽象工厂完成相同的事情,仅通过一行专门的 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

推荐答案

Rob Olmos 解释得对:

注解基本上可以让你注入行为,并且可以促进解耦.

Annotations basically let you inject behavior and can promote decoupling.

用我的话来说,这些注释是有价值的,尤其是在您收集的reflection(附加) 有关您正在检查的类/方法/属性的元数据.

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/annotations 来识别哪些对象被注入到从 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.

以下过于简化的示例:

你有两个职业,一个Soldier职业和一个Weapon职业.Weapon 实例被注入到 Soldier 实例中.看一下两个类的定义:

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();
    }
}

如果您要使用这个类并手动注入所有依赖项,您会这样做:

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();

好吧,那是很多样板代码(请耐心等待,我很快就会解释注释的用途).依赖注入框架可以为您做的是抽象创建这样的组合对象并自动注入所有依赖项,您只需:

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

是的,但是 Container 必须知道 Soldier 类具有哪些依赖项.因此,大多数常见框架都使用 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 代码中的注释,以便定义这些依赖项.在 FLOW3 中,您的 Soldier 类将如下所示(语法仅作为示例):

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 容器的 SoldierWeapon 的依赖关系.

So, no XML required to mark the dependency of Soldier to Weapon for the DI container.

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 代码中,而不是使用单独的文件.

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天全站免登陆