About annotations

What is an annotation?

The annotation is actually the annotation in the PHP code, but it has a functional meaning.

use Swoft\Http\Server\Annotation\Mapping\Controller;
use Swoft\Http\Server\Annotation\Mapping\RequestMapping;
use Swoft\Bean\Annotation\Mapping\Inject;

/**
 * 类注释
 *
 * @since 2.0
 * 
 * @Controller("a")
 */
class A
{
    /**
     * 属性注释
     *
     * @Inject()
     *
     * @var string
     */
    private $pro = '';

    /**
     * @RequestMapping()
     * 
     * 方法注释
     */
    public function method(): void
    {

    }
}
  • @ControllerNote that defines a controller that is described in more detail in subsequent chapters.
  • @InjectNote that defines a dependency that is described in more detail in subsequent chapters.
  • @RequestMappingNote that defines an action that is described in more detail in subsequent chapters.

A critical warning ⚠️ annotation must be /** started and */ ended. Otherwise it will result in parsing!

Principle

Specification

PHP code itself will have a lot of comments, developers write a functional annotation, if random scribbling, will lead to bad maintenance. Swoft frame source code, annotation specifications are as follows:

  • Class annotation, after all class comments
  • Property annotations, after property descriptions, before other comments
  • Method annotation, after the method description, before the other comments

The annotation specification inside the Swoft framework is for reference only, and if there is a better way, the discussion is welcome.

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