Phalcon的注解(Phalcon\Annotations\*)
- Abstract class Phalcon\Annotations\Adapter
- Class Phalcon\Annotations\Adapter\Apc
- Class Phalcon\Annotations\Adapter\Files
- Class Phalcon\Annotations\Adapter\Memory
- Class Phalcon\Annotations\Adapter\Xcache
- Class Phalcon\Annotations\Annotation
- Class Phalcon\Annotations\Collection Annotation集合
- Class Phalcon\Annotations\Exception
- Class Phalcon\Annotations\Reader 读取类的注解,以数组形式分类返回, 可用于创建 Reflection
- Class Phalcon\Annotations\Reflection
Reflection
注解反射对象, 获取类方法,属性,类等的注释
常用方法, 返回 Collection, 可遍历的对象集合(Annotation)
1 2 |
public getClassAnnotations (); public getMethodsAnnotations (); |
Reflection的使用参考:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 |
use Phalcon\Annotations\Adapter\Memory as MemoryAdapter; use Phalcon\Annotations\Reader; use Phalcon\Annotations\Reflection as PhalconReflection; function test2($useCache = 1){ if($useCache == 1) { $reader = new MemoryAdapter(); $reflection = $reader->get('TestObject'); } else { // Parse the annotations in a class $reader = new Reader(); $parsing = $reader->parse('TestObject'); // Create the reflection $reflection = new PhalconReflection($parsing); } // Get the annotations in the class docblock $classAnnotations = $reflection->getClassAnnotations(); var_dump($classAnnotations); $methodAnnotations = $reflection->getMethodsAnnotations(); var_dump($methodAnnotations); } |
Annotation 注解类
获取单个注解的名称、参数表达式等
注解示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
<?php /** * Simple Annotation * * @SomeAnnotation */ /** * Annotation with parameters * * @SomeAnnotation("hello", "world", 1, 2, 3, false, true) */ /** * Annotation with named parameters * * @SomeAnnotation(first="hello", second="world", third=1) * @SomeAnnotation(first: "hello", second: "world", third: 1) */ /** * Passing an array * * @SomeAnnotation([1, 2, 3, 4]) * @SomeAnnotation({1, 2, 3, 4}) */ /** * Passing a hash as parameter * * @SomeAnnotation({first=1, second=2, third=3}) * @SomeAnnotation({'first'=1, 'second'=2, 'third'=3}) * @SomeAnnotation({'first': 1, 'second': 2, 'third': 3}) * @SomeAnnotation(['first': 1, 'second': 2, 'third': 3]) */ /** * Nested arrays/hashes * * @SomeAnnotation({"name"="SomeName", "other"={ * "foo1": "bar1", "foo2": "bar2", {1, 2, 3}, * }}) */ /** * Nested Annotations * * @SomeAnnotation(first=@AnotherAnnotation(1, 2, 3)) */ |
PHP相关工具参考:
doctrine/annotations
https://github.com/doctrine/annotations
PHP反射相关接口参考:
http://php.net/manual/zh/book.reflection.php