企业🤖AI智能体构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
**一.注释定义** ``` /** * @api http://www.yzmedu.com/api */ function show($name){} ```   **二.获取注释** ``` $ref=new ReflectionFunction("show"); $doc=$ref->getDocComment(); $api=substr($doc, strpos($doc, "@api") + strlen("@api "),-2); ```   **三.Attributes语法** ``` #[Name] #[Name1,Name2] #[Name(Arguments)] #[Name(Argunment1,Arguments2,ArgumentN)] #[Name1(Argument),Name2(Argument),Name3(Argument)] ```   **四.注解定义** 1.单参数 ``` #[api("http://www.yzmedu.com/api")] function show($name){} ``` 2.多参数 ``` #[myattr("api","http://www.yzmedu.com/api")] #[api("http://www.yzmedu.com/api")] ```   **五.获取注解** ``` $ref=new ReflectionFunction("show"); $attr=$ref->getAttributes("api")[0]; $name=$attr->getName(); $value=$attr->getArguments(); ```   **六.ReflectionAttribute类结构** ``` final class ReflectionAttribute { /** * @return string The name of the attribute, with class names resolved. */ public function getName(): string {} /** * @return array Arguments passed to the attribute when it is declared. */ public function getArguments(): array {} /** * @return object An instantiated class object of the name, with arguments passed to the constructor. */ public function newInstance(): object {} } ```   **七.函数注解类使用** ``` #[Attribute(Attribute::TARGET_FUNCTION)] class MyAttr{ public function __construct($name, $value) { echo "$name,$value"; } } #[MyAttr("api","http://www.yzmedu.com/api")] function show(){} $ref=new ReflectionFunction("show"); $attr=$ref->getAttributes("MyAttr")[0]->newInstance(); ```   **八.注解类类型** ``` TARGET_CLASS //类的注解类 TARGET_FUNCTION //函数注解类 TARGET_METHOD //方法注解类 TARGET_PROPERTY //属性注解类 TARGET_CLASS_CONSTANT //类常量注解类 TARGET_PARAMETER //参数注解类 TARGET_ALL ```   **九.函数注解类高级配置** ``` #[Attribute(Attribute::TARGET_FUNCTION)] class Myattr{ public $name; public $value; public $number; public function __construct($name,$value,$number){ $this->name=$name; $this->value=$value; $this->number=$number; } public function say(){ echo "{$this->name}--{$this->value}--{$this->number}"; } } #[Myattr('api','http://www.yzmedu.com/api','10')] function show(){} $ref=new ReflectionFunction("show"); $attr=$ref->getAttributes('Myattr')[0]; $obj=$attr->newInstance(); $obj->say(); ```   **十.类注解类使用** ``` #[Attribute(Attribute::TARGET_CLASS)] class MyAttr{ public function __construct($name, $value) { echo "$name,$value"; } } #[MyAttr("api","http://www.yzmedu.com/api")] class Person{} $ref=new ReflectionClass("Person"); $ref->getAttributes("MyAttr")[0]->newInstance(); ```   **十一.类注解高级配置** ``` #[Attribute(Attribute::TARGET_CLASS)] class Myattr{ public $name; public $value; public $number; public function __construct($name,$value,$number){ $this->name=$name; $this->value=$value; $this->number=$number; } public function say(){ echo "{$this->name}--{$this->value}--{$this->number}"; } } #[Myattr('api','http://www.yzmedu.com/api','10')] class Person{} $ref=new ReflectionClass("Person"); $attr=$ref->getAttributes('Myattr')[0]; $obj=$attr->newInstance(); $obj->say(); ```   ### **系统的学习PHP** 关注:PHP自学中心,回复相应的关键词,领取以下视频教程 **Redis6.0全套实战教程【面试题讲解】** 公众号里回复:884613   #### **还有其他的教程的关键词,请关注公众号查看每天分享的文章教程的头部** ![](https://img.kancloud.cn/96/af/96af322d2cdc53d3fbbe981affa60c7f_150x150.jpg)