annotation

installation

 composer require swoft/annotation 

What is an annotation

The annotation is actually a comment in the PHP code, but it has 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
    {

    }
} 
  • @Controller annotation defines a controller, which is described in detail in subsequent chapters.
  • @Inject annotation defines a dependency, which is described in detail in subsequent chapters.
  • @RequestMapping annotation defines an action, which is described in detail in subsequent chapters.

Serious warnings ⚠️ annotations must start with /** and end with */ . Otherwise it will not be resolved!

principle

specification

PHP code itself will have a lot of comments, developers have written functional annotations, if you arbitrarily scribble, it will lead to poor maintenance. The Swoft framework source code, the annotation specifications are as follows:

  • Class annotation, after all class comments
  • Attribute annotation, after attribute description, before other comments
  • Method annotation, method description, before other comments

The annotation specifications in the Swoft framework are for reference only, and if there is a better way, welcome to discuss.

Git warehouse

Participate in the contribution

Welcome to participate in the contribution, you can

  • Fork our development warehouse swewt/component
  • Modify the code and then initiate the PR
  • About PR launch of Notes
/docs/2.x/en/annotation/index.html
progress-bar