3

PHP 8新特性之Attributes(注解)

 3 years ago
source link: https://www.laruence.com/2020/06/12/5902.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

PHP 8新特性之Attributes(注解)

PHP8的Alpha版本,过几天就要发布了,其中包含了不少的新特性,当然我自己认为最重要的还是JIT,这个我从2013年开始参与,中间挫折无数,失败无数后,终于要发布的东东。

不过,今天呢,我不打算谈JIT,等PHP8发布了以后,我再单独写个类似《深入理解PHP8之JIT》系列来说吧。

嘿嘿,今天呢,我想谈谈Attributes,为啥呢, 是昨天我看到很多群在转发一个文章,叫做《理解PHP8中的Attributes》,说实在的,这篇文章应该是直接从英文翻译过来的,写的晦涩难懂,很多同学看完以后表示,看的一头雾水,不知道在说啥。

于是我想,就用一篇文章来简单说说这是个啥。

后记(2020/09/07), 最新的RFC投票通过,把注解的符号从<<>>, 改成了#[], 所以此文做了更新,使用最新的注解符号

说注解之前,先说说以前的注释,我们经常会在PHP的项目中,看到的一个东西,类似如下的@param 和 @see :

  1. * @param Foo $argument
  2. * @see https:/xxxxxxxx/xxxx/xxx.html
  3.  function dummy($Foo) {}

这个叫做注释,对于以前的PHP来说,注释中的@param和@see毫无意义,整个这一段会保存为一个函数/方法的一个叫做doc_comment的字符串。

如果我们要分析这段注释的含义,我们需要通过设计一些特定的语法,就比如栗子中的@+name, 类似@param一样, 然后自己分析这段字符串,来提取对应的信息。

比如我们要获取See这个注释的信息,我们需要做类似:

  1. $ref = new ReflectionFunction("dummy");
  2. $doc = $ref->getDocComment();
  3. $see = substr($doc, strpos($doc, "@see") + strlen("@see "));

这样的字符串处理,相对比较麻烦,也比较容易出错。

而Attributes呢,其实就是把“注释”升级为支持格式化内容的“注解”

比如上面的例子:

  1. #[Params("Foo", "argument")]
  2. #[See("https://xxxxxxxx/xxxx/xxx.html")]
  3. function dummy($argument) {}

当有多个注解的时候,你也可以写成:

  1.  Params("Foo", "argument"),
  2.  See("https://xxxxxxxx/xxxx/xxx.html")
  3. function dummy($argument) {}

大家不要纠结这么写的意义是啥,从功能上来说,现在你就可以通过Reflection来获取这段格式化的注解了,比如, 我们现在要获取See这个注解:

  1. $ref = new ReflectionFunction("dummy");
  2. var_dump($ref->getAttributes("See")[0]->getName());
  3. var_dump($ref->getAttributes("See")[0]->getArguments());
  1. string(3) "See"
  2. array(1) {
  3.   [0]=>
  4.   string(30) "https://xxxxxxxx/xxxx/xxx.html"

当然,还有稍微高级一点的用法,就是你可以定义一个所谓的“注解类”:

  1. <?php
  2. #[Attribute(Attribute::TARGET_FUNCTION)]
  3. class MyAttribute {
  4.      public function __construct($name, $value) {
  5.           var_dump($name);
  6.           var_dump($value);

然后, 你就可以写类似, 注意其中的newInstance调用:

  1. #[MyAttribute("See", "https://xxxxxxxx/xxxx/xxx.html")]
  2. function dummy($argument) {
  3. $ref = new ReflectionFunction("dummy");
  4. $ref->getAttributes("MyAttribute")[0]->newInstance();

如果你跑这段代码,你会看到MyAttribute的__construct方法被调用了, 调用传递的参数就是”See”和”https://xxx”

明白了么, 就是你可以把一个注解“实例化”, 然后,你就可以基于这个能力,来做自己的“注释即配置”的设计。

总结下Attributes的写法就是如下的形式:

  1. #[Name]
  2. #[Name(Arguments)]
  3. #[Name(Argunment1, Arguments2, ArgumentN)]
  4. #[Name1(Argument), Name2(Argument), Name3(Argument)]

然后你就可以通过PHP的Reflection系列的方法,根据getAttributes("Name")获取对应的注解, 进一步你可以通过调用返回的注解的getName方法获取名字,getArguments方法获取括号中的Arguments。

再进一步,如果Name是一个你自己定义的注解类,通过#[Attribute(Attribute::TARGET_FUNCTION)], 或者:

  1. TARGET_CLASS //类的注解类
  2. TARGET_FUNCTION //函数注解类
  3. TARGET_METHOD //方法注解类
  4. TARGET_PROPERTY //属性注解类
  5. TARGET_CLASS_CONSTANT //类常量注解类
  6. TARGET_PARAMETER //参数注解类
  7. TARGET_ALL

来表明注解类应用的目标类型,然后你就可以调用newInstance方法,实现类似"new Name(Arguments)"的调用。

也许很多人会问,这有什么卵用?

坦白说,我一直对新特性无感,但这个Attributes,多少还是应该有那么一点点吧 🙂


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK