注解在 PHP 中有何用处?

2022-01-13 00:00:00 annotations 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.– Hopeeekr 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 使用直接在 PHP 代码中而不是 XML 的注释来定义这些依赖关系.在 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.

相关文章