PHP 注解

到目前为止,PHP的反射特性中是不支持注解Annotation的,但是可以支持基本的文档注释内容的获取 ReflectionMethod::getDocComment() - 从5.1.0开始 。PHP的反射其实已经挺强大的了,只要再进一步,解析文档注释中的相关注解内容即可。

AppServer.io 提供了一个lang库,实现了对注解的支持。其中还运用了PHP的Tokenizer特性来解析注解代码,具体原理不详述,有兴趣自行阅读代码。

https://github.com/appserver-io/lang

其关于注解的说明见:http://appserver.io/get-started/documentation/annotations.html

在此摘录其演示代码如下:

<?php

namespace Namespace\Module;

use AppserverIo\Appserver\Lang\Reflection\ReflectionClass;
use AppserverIo\Appserver\Lang\Reflection\ReflectionAnnotation; class Route extends ReflectionAnnotation
{ /**
* Returns the value of the name attribute.
*
* @return string The annotations name attribute
*/
public function getPattern()
{
return $this->values['pattern'];
}
} class IndexController
{ /**
* Default action implementation.
*
* @return void
* @Route(pattern="/index/index")
*/
public function indexAction()
{
// do something here
}
} // create a reflection class to load the methods annotation
$reflectionClass = new ReflectionClass('IndexController');
$reflectionMethod = $reflectionClass->getMethod('indexAction');
$reflectionAnnotation = $reflectionMethod->getAnnotation('Route');
$pattern = $reflectionAnnotation->newInstance()->getPattern();

通过这种特性,可以实现注解的方式指定方法的url路由模式 /index/index

自行实现PHP代码注解特性的更多相关文章

  1. 7.7 数据注解特性--Table

    大家可能注意到,有几个特性,我没有翻译,因为实在是太简单了,看一下就知道,之前也学过,现在只是系统学一下,所以就粗略的看一下就行了. 现在学习数据注解特性的--Table特性. Table 特性可以被 ...

  2. 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...

  3. 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...

  4. 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...

  5. 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...

  6. 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】

    原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...

  7. 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)

    原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...

  8. 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】

    原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...

  9. 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】

    原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...

随机推荐

  1. EF架构~通过EF6的DbCommand拦截器来实现数据库读写分离~终结~配置的优化和事务里读写的统一

    回到目录 本讲是通过DbCommand拦截器来实现读写分离的最后一讲,对之前几篇文章做了一个优化,无论是程序可读性还是实用性上都有一个提升,在配置信息这块,去除了字符串方式的拼接,取而代之的是sect ...

  2. Oracle使用小记

    windows下Oracle必须要启动的服务 Oracle ORCL VSS Writer Service:Oracle卷映射拷贝写入服务,VSS(Volume Shadow Copy Service ...

  3. Maven项目环境搭建实例.

    前言:最近下班比较早, 总是不愿意让自己闲着, 此时刚好从网上找到了一些项目的资源, 结合自己在公司做的项目, 所以拿来一起学习加复习一些平常用到和没接触过的新知识.做的这个项目的名称叫做babasp ...

  4. Atitit hsv转grb  应该优先使用hsv颜色原则 方便人类

    Atitit hsv转grb  应该优先使用hsv颜色原则 方便人类 1.1. 1.1.hsv色卡1 1.2. 从 HSV 到 RGB 的转换1 1.3. HSVtoRGBColorV22 1.1.  ...

  5. Android 常见Crash Log汇总

    一.BinderProxy@4479b390 is not valid; is your activity running? 原因分析: 因为使用了AsyncTask 异步线程在线程完成以后的onPo ...

  6. inoic start projectname sidemenu报错 - Error: Cannot find module 'lodash._baseslice'

    inoic start projectname sidemenu报错 - Error: Cannot find module 'lodash._baseslice' 在公司的电脑上出现过这个错误,后来 ...

  7. HDU 1024Max Sum Plus Plus(最大m字段和)

    /* 动态转移方程:dp[i][j]=max(dp[i-1]+a[i], max(dp[t][j-1])+a[i]) (j-1<=t<i) 表示的是前i个数j个字段和的最大值是多少! */ ...

  8. linux分享六:字符串处理

    一:cut (1)其语法格式为:cut [-bn] [file] 或 cut [-c] [file] 或 cut [-df] [file] 使用说明cut 命令从文件的每一行剪切字节.字符和字段并将这 ...

  9. 优秀教程:使用 CSS3 动画实现的超炫的过渡特效

    Codrops 最近分享了一些很酷的图片切换灵感.有三种不同的用例:小的图像幻灯片,大标题幻灯片以及使用透明背景的产品幻灯片.状态转换使用 CSS 动画完成,我们能够定义从任何方向进来的图片的行为. ...

  10. Hover.css:一组超实用的 CSS3 悬停效果和动画

    Hover.css 是一套基于 CSS3 的鼠标悬停效果和动画,这些可以非常轻松的被应用到按钮.LOGO 以及图片等元素.所有这些效果都是只需要单一的标签,必要的时候使用 before 和 after ...