rfc:annotations_v2

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Next revision
Previous revision
rfc:annotations_v2 [2019/02/05 22:30] – created draft version brzuchalrfc:annotations_v2 [2020/07/22 08:44] (current) brzuchal
Line 1: Line 1:
-====== PHP RFC: Annotations ======+====== PHP RFC: Annotations 2.0 ======
   * Version: 0.1   * Version: 0.1
   * Date: 2019-02-05   * Date: 2019-02-05
   * Author: Michał Brzuchalski <michal.brzuchalski@gmail.com>   * Author: Michał Brzuchalski <michal.brzuchalski@gmail.com>
-  * Status: Draft+  * Status: Obsolete
   * First Published at: http://wiki.php.net/rfc/annotations_v2   * First Published at: http://wiki.php.net/rfc/annotations_v2
  
Line 21: Line 21:
  
 <code php> <code php>
-@AnnotationName("value", namedParamter=true, embeded=@EmbededAnnotation)+[@AnnotationName("value", true)
 +[SimpleAnnotation] 
 +class Foo {}
 </code> </code>
  
Line 34: Line 36:
 use ORM\Column; use ORM\Column;
  
-@Entity +[@Entity] 
-@Table("foo")+[@Table("foo")]
 class Foo { class Foo {
-    @Id @Column("id", type="uuid")+    [@Id
 +    [@Column("id", "uuid")]
     private $id;     private $id;
 } }
Line 46: Line 49:
  
 class FooController { class FooController {
-    @Route("/api/foo", methods=["POST"], name="foo_create")+    [@Route("/api/foo", ["POST"], "foo_create")]
     public function create(Request $request): Response     public function create(Request $request): Response
     {     {
Line 54: Line 57:
 </code> </code>
  
-==== Built in annotations ====+==== Built-in annotations ====
  
-=== @Annotation ===+First, there are several that inform compilation: 
 + 
 +=== [Compiled] === 
 +Function scoped annotation indicating if a function should be JIT compiled. 
 + 
 +=== [SupressWarnings] === 
 +Function or statement scoped annotation indicating if error supression should be applied. 
 + 
 +==== Meta-Annotations ==== 
 + 
 +Next, meta-annotations are annotations that can be applied to other annotations. 
 + 
 +For example, these meta-annotations are used for annotation configuration: 
 + 
 +=== [@Annotation===
 Annotation classes have to contain a ''@Annotation''. Annotation classes have to contain a ''@Annotation''.
  
 <code php> <code php>
-@Annotation+[@Annotation]
 class MyAnnotation { class MyAnnotation {
     // some code     // some code
Line 66: Line 83:
 </code> </code>
  
-=== @Target ===+=== [@Target===
 ''@Target'' annotation indicates the kinds of the class element or a function which an annotation type is applicable. ''@Target'' annotation indicates the kinds of the class element or a function which an annotation type is applicable.
 Then you could define one or more targets: Then you could define one or more targets:
  
-* ''CLASS'' allowed before the class declaration +  * ''CLASS'' allowed before the class declaration 
-* ''PROPERTY'' allowed before class property declaration +  * ''PROPERTY'' allowed before class property declaration 
-* ''METHOD'' allowed before the class method declaration +  * ''METHOD'' allowed before the class method declaration 
-* ''FUNCTION'' allowed before the function declaration +  * ''FUNCTION'' allowed before the function declaration 
-* ''ALL'' allowed in all cases, also default value +  * ''ALL'' allowed in all cases, also default value 
-* ''ANNOTATION'' allowed before annotation class declaration+  * ''ANNOTATION'' allowed before annotation class declaration
  
-=== @Repeatable ===+=== [@Repeatable===
 ''@Repeatable'' annotation indicates the annotation may be repeated multiple times when annotating. ''@Repeatable'' annotation indicates the annotation may be repeated multiple times when annotating.
  
-=== @Inherited ===+=== [@Inherited===
 ''@Inherited'' annotation can be used as meta-annotation on the other user-defined annotation classes.  ''@Inherited'' annotation can be used as meta-annotation on the other user-defined annotation classes. 
 When such user-defined annotations are used on superclass, they are automatically inherited to subclasses. When such user-defined annotations are used on superclass, they are automatically inherited to subclasses.
  
 <code php> <code php>
-@Annotation+[@Annotation]
 class MyAnnotation {} class MyAnnotation {}
  
-@Annotation +[@Annotation] 
-@Inherited+[@Inherited]
 class MyInheritedAnnotation {} class MyInheritedAnnotation {}
  
-@MyAnnotation +[@MyAnnotation] 
-@MyInheritedAnnotation+[@MyInheritedAnnotation]
 class Foo {} class Foo {}
  
Line 101: Line 118:
 $classAnnotations = $refl->getAnnotations(); // will include @MyInheritedAnnotation only $classAnnotations = $refl->getAnnotations(); // will include @MyInheritedAnnotation only
 </code> </code>
- 
-===== Examples ===== 
  
 ==== Custom annotations ==== ==== Custom annotations ====
-Using annotations 
  
-<code php> +Annotation type declarations are similar to normal class declarations
-use Example\MyAnnotation; +Each property declaration defines an element of the annotation type. 
-use Example\MyEmbededAnnotation; +Property types are restricted to primitives or another annotation type.
- +
-@MyAnnotation( +
-    myProperty="value",  +
-    myArrayProperty=[1, 3.14, true, "string", DIRECTORY_SEPARATOR],  +
-    myEmbeded=@MyEmbededAnnotation() +
-+
-class Foo { +
-    @MyPropertyAnnotation +
-    private $property; +
-   +
-    @MyMethodAnnotation("value"+
-    public function bar() {} +
-+
-</code>+
  
-Declaring custom annotations+Declaring custom annotations:
  
 <code php> <code php>
 namespace Example; namespace Example;
  
-@Annotation +[@Annotation] 
-@Target("class")+[@Target("class")]
 class MyAnnotation { class MyAnnotation {
-    @Required+    [@Required]
     public string $myProperty;     public string $myProperty;
     public array $myArrayProperty = [];     public array $myArrayProperty = [];
Line 139: Line 139:
 } }
  
-@Annotation +[@Annotation] 
-@Target(["class", "annotation"])+[@Target(["class", "annotation"])]
 class MyEmbededAnnotation { class MyEmbededAnnotation {
 } }
  
-@Annotation +[@Annotation] 
-@Target("property")+[@Target("property")]
 class MyPropertyAnnotation { class MyPropertyAnnotation {
 } }
Line 156: Line 156:
         $this->value = $value;         $this->value = $value;
     }     }
 +}
 +</code>
 +
 +Using annotations
 +
 +<code php>
 +use Example\MyAnnotation;
 +use Example\MyEmbededAnnotation;
 +
 +[@MyAnnotation([
 +    "myProperty" => "value", 
 +    "myArrayProperty" = > [1, 3.14, true, "string", DIRECTORY_SEPARATOR], 
 +)]
 +class Foo {
 +    [@MyPropertyAnnotation]
 +    private $property;
 +  
 +    [@MyMethodAnnotation("value")]
 +    public function bar() {}
 } }
 </code> </code>
rfc/annotations_v2.txt · Last modified: 2020/07/22 08:44 by brzuchal