{"id":59,"date":"2015-09-18T11:58:20","date_gmt":"2015-09-18T03:58:20","guid":{"rendered":"http:\/\/blog.inforere.com\/?p=59"},"modified":"2016-09-21T17:02:57","modified_gmt":"2016-09-21T09:02:57","slug":"phalcon%e7%9a%84%e6%b3%a8%e8%a7%a3phalconannotations","status":"publish","type":"post","link":"https:\/\/blog.inforere.com\/?p=59","title":{"rendered":"\u6ce8\u89e3\u7684\u89e3\u6790"},"content":{"rendered":"<h3>Phalcon\u7684\u6ce8\u89e3(Phalcon\\Annotations\\*)<\/h3>\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Adapter.html\">Abstract class <strong>Phalcon\\Annotations\\Adapter<\/strong><\/a><\/li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Adapter_Apc.html\">Class <strong>Phalcon\\Annotations\\Adapter\\Apc<\/strong><\/a><\/li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Adapter_Files.html\">Class <strong>Phalcon\\Annotations\\Adapter\\Files<\/strong><\/a><\/li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Adapter_Memory.html\">Class <strong>Phalcon\\Annotations\\Adapter\\Memory<\/strong><\/a><\/li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Adapter_Xcache.html\">Class <strong>Phalcon\\Annotations\\Adapter\\Xcache<\/strong><\/a><\/li>\n<\/ul>\n<ul>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Annotation.html\">Class <strong>Phalcon\\Annotations\\Annotation<\/strong><\/a><\/li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Collection.html\">Class <strong>Phalcon\\Annotations\\Collection<\/strong><\/a>\u00a0Annotation\u96c6\u5408<\/li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Exception.html\">Class <strong>Phalcon\\Annotations\\Exception<\/strong><\/a><\/li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Reader.html\">Class <strong>Phalcon\\Annotations\\Reader<\/strong><\/a>\u00a0\u8bfb\u53d6\u7c7b\u7684\u6ce8\u89e3\uff0c\u4ee5\u6570\u7ec4\u5f62\u5f0f\u5206\u7c7b\u8fd4\u56de, \u53ef\u7528\u4e8e\u521b\u5efa\u00a0Reflection<\/li>\n<li class=\"toctree-l1\"><a class=\"reference internal\" href=\"https:\/\/docs.phalconphp.com\/en\/latest\/api\/Phalcon_Annotations_Reflection.html\">Class <strong>Phalcon\\Annotations\\Reflection<\/strong><\/a><\/li>\n<\/ul>\n<p>&nbsp;<\/p>\n<p><strong>Reflection<br \/>\n\u6ce8\u89e3\u53cd\u5c04\u5bf9\u8c61\uff0c \u83b7\u53d6\u7c7b\u65b9\u6cd5\uff0c\u5c5e\u6027\uff0c\u7c7b\u7b49\u7684\u6ce8\u91ca<\/strong><\/p>\n<p>\u5e38\u7528\u65b9\u6cd5, \u8fd4\u56de <strong>Collection<\/strong>, \u53ef\u904d\u5386\u7684\u5bf9\u8c61\u96c6\u5408(Annotation)<\/p>\n<pre class=\"lang:php decode:true\">\u00a0 public getClassAnnotations ();\r\n\u00a0 public getMethodsAnnotations ();<\/pre>\n<p>Reflection\u7684\u4f7f\u7528\u53c2\u8003\uff1a<\/p>\n<pre class=\"lang:php decode:true \">use Phalcon\\Annotations\\Adapter\\Memory as MemoryAdapter;\r\nuse Phalcon\\Annotations\\Reader;\r\nuse Phalcon\\Annotations\\Reflection as PhalconReflection;\r\n\r\nfunction test2($useCache = 1){\r\n    if($useCache == 1) {\r\n        $reader = new MemoryAdapter();\r\n        $reflection = $reader-&gt;get('TestObject');\r\n    } else {\r\n        \/\/ Parse the annotations in a class\r\n        $reader = new Reader();\r\n        $parsing = $reader-&gt;parse('TestObject');\r\n        \/\/ Create the reflection\r\n        $reflection = new PhalconReflection($parsing);\r\n    }\r\n\r\n    \/\/ Get the annotations in the class docblock\r\n    $classAnnotations = $reflection-&gt;getClassAnnotations();\r\n    var_dump($classAnnotations);\r\n\r\n    $methodAnnotations = $reflection-&gt;getMethodsAnnotations();\r\n    var_dump($methodAnnotations);\r\n}<\/pre>\n<p><strong>Annotation\u00a0<\/strong>\u6ce8\u89e3\u7c7b<br \/>\n\u83b7\u53d6\u5355\u4e2a\u6ce8\u89e3\u7684\u540d\u79f0\u3001\u53c2\u6570\u8868\u8fbe\u5f0f\u7b49<\/p>\n<p><b>\u6ce8\u89e3\u793a\u4f8b<\/b><\/p>\n<pre class=\"lang:default decode:true\">&lt;?php\r\n\r\n\/**\r\n * Simple Annotation\r\n *\r\n * @SomeAnnotation\r\n *\/\r\n\r\n\/**\r\n * Annotation with parameters\r\n *\r\n * @SomeAnnotation(\"hello\", \"world\", 1, 2, 3, false, true)\r\n *\/\r\n\r\n\/**\r\n * Annotation with named parameters\r\n *\r\n * @SomeAnnotation(first=\"hello\", second=\"world\", third=1)\r\n * @SomeAnnotation(first: \"hello\", second: \"world\", third: 1)\r\n *\/\r\n\r\n\/**\r\n * Passing an array\r\n *\r\n * @SomeAnnotation([1, 2, 3, 4])\r\n * @SomeAnnotation({1, 2, 3, 4})\r\n *\/\r\n\r\n\/**\r\n * Passing a hash as parameter\r\n *\r\n * @SomeAnnotation({first=1, second=2, third=3})\r\n * @SomeAnnotation({'first'=1, 'second'=2, 'third'=3})\r\n * @SomeAnnotation({'first': 1, 'second': 2, 'third': 3})\r\n * @SomeAnnotation(['first': 1, 'second': 2, 'third': 3])\r\n *\/\r\n\r\n\/**\r\n * Nested arrays\/hashes\r\n *\r\n * @SomeAnnotation({\"name\"=\"SomeName\", \"other\"={\r\n *     \"foo1\": \"bar1\", \"foo2\": \"bar2\", {1, 2, 3},\r\n * }})\r\n *\/\r\n\r\n\/**\r\n * Nested Annotations\r\n *\r\n * @SomeAnnotation(first=@AnotherAnnotation(1, 2, 3))\r\n *\/<\/pre>\n<p>&nbsp;<\/p>\n<h3>PHP\u76f8\u5173\u5de5\u5177\u53c2\u8003\uff1a<\/h3>\n<p>doctrine\/annotations<br \/>\nhttps:\/\/github.com\/doctrine\/annotations<\/p>\n<h3>PHP\u53cd\u5c04\u76f8\u5173\u63a5\u53e3\u53c2\u8003\uff1a<\/h3>\n<p>http:\/\/php.net\/manual\/zh\/book.reflection.php<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Phalcon\u7684\u6ce8\u89e3(Phalcon\\Annotations\\*) Abstract class Phalco [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[6],"tags":[22],"_links":{"self":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/59"}],"collection":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=59"}],"version-history":[{"count":9,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions"}],"predecessor-version":[{"id":254,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=\/wp\/v2\/posts\/59\/revisions\/254"}],"wp:attachment":[{"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=59"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=59"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.inforere.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=59"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}