use

The annotation is very simple to use. Just like using a class, first introduce the class with the use keyword, then use the annotation.

 use Swoft\Http\Server\Annotation\Mapping\Controller; 

/**
 * 类注释
 *
 * @since 2.0
 * 
 * @Controller("a")
 */
class A
{
} 
  • Line 1 introduces an annotation class, like a normal class
  • Line 8 uses annotations, format @类名 , and some class names will have parameters, such as "a" this annotation.

principle

Why does PHP's normal comments have functionality? In fact, it's very simple. Get all the annotations in the class via PHP reflection ( https://www.php.net/manual/zh/book.reflection.php ), followed by the PHP component ( https://github.com/doctrine/ Annotations make it special.

/docs/2.x/en/annotation/usage.html
progress-bar