0
点赞
收藏
分享

微信扫一扫

sowft注解

young_d807 2023-10-28 阅读 16

一、概念:

  1. 为什么要使用注解?

①. PHP是没有注解,所以只能用注释来模拟. ②. 借鉴了Java spring的思想. ③. 一般框架会使用Doctrine Annotations库: https://github.com/doctrine/annotations ④. swoole是常驻内存的,使用IoC容器结合注解特别有意义.1.1 反射实现注解原理:

PHP中利用反射(getDocComment)来读取类或方法上的注释内容(一堆字符串),再正则或其它方式解析出"有规则的内容".

①. 获取文档注释:
public ReflectionClass::getDocComment(void): string

②. 举例:
/**
* This is an Example class
*/
class Example
{
    /**
     * This is an example function
     */
    public function fn() {}
}
$reflector = new ReflectionClass('Example');
echo $reflector->getDocComment();                 # // to get the Class DocBlock
$reflector->getMethod('fn')->getDocComment();     # // to get the Method DocBlock

举报

相关推荐

0 条评论