自行实现PHP代码注解特性
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代码注解特性的更多相关文章
- 7.7 数据注解特性--Table
大家可能注意到,有几个特性,我没有翻译,因为实在是太简单了,看一下就知道,之前也学过,现在只是系统学一下,所以就粗略的看一下就行了. 现在学习数据注解特性的--Table特性. Table 特性可以被 ...
- 9.10 翻译系列:EF数据注解特性之StringLength【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/stringlength-dataannotations-attribute-in-co ...
- 9.9 翻译系列:数据注解特性之--MaxLength 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/maxlength-minlength-dataannotations-attribut ...
- 9.7 翻译系列:EF数据注解特性之--InverseProperty【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/inverseproperty-dataannotations-attribute-in ...
- 9.3 翻译系列:数据注解特性之Key【EF 6 Code-First 系列】
原文链接:http://www.entityframeworktutorial.net/code-first/key-dataannotations-attribute-in-code-first.a ...
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
- 9.翻译系列:EF 6以及EF Core中的数据注解特性(EF 6 Code-First系列)
原文地址:http://www.entityframeworktutorial.net/code-first/dataannotation-in-code-first.aspx EF 6 Code-F ...
- 9.1 翻译系列:数据注解特性之----Table【EF 6 Code-First 系列】
原文地址:http://www.entityframeworktutorial.net/code-first/table-dataannotations-attribute-in-code-first ...
- 9.8 翻译系列:数据注解特性之--Required 【EF 6 Code-First系列】
原文链接:https://www.entityframeworktutorial.net/code-first/required-attribute-dataannotations-in-code-f ...
随机推荐
- DDD~我们应该知道的Model,DomainModel和ViewModel
回到目录 图在前 目前项目中可能出现的三种Model模式,对于我们现在开发的一个项目,我觉得使用DDD的思想来设计模型比较清晰,使用DDD的思想把模型model分成了如下三种: 下面是我微博中的截 ...
- android 股票数据通过日K获取周K的数据 算法 源码
目前的数据是从新浪接口获取的, http://biz.finance.sina.com.cn/stock/flash_hq/kline_data.php?symbol=sh600000&end ...
- Python无聊的总结
在公司无聊的时候看了前辈写的python代码,突然发现一个比较好玩的python表达式: lambda x,y:x+y 咋一看,这个应该类似方法之类的,上网查了查,所以特此总结下 lambda:上代码 ...
- Android入门(十九)WebView
原文链接:http://www.orlion.ga/676/ WebView可以在自己的应用程序中嵌入一个浏览器来展示网页. 创建一个项目WebViewDemo,修改activity_main.xml ...
- 【5.1送礼】国内第一部Matlab和C#.Net混合编程视频教程【免费】
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 Matlab和C#混合编程文章目录 :[目录]Matlab和C#混合编程文章目录 上一次写博客很久了 ...
- bootstrap插件思路整理
知识有时也需温故知新嘛,本次做一次bs插件梳理. $.support.transition 通过判断自定义元素是否支持WebkitTransition.MozTransition.OTransitio ...
- Net连接mysql的公共Helper类MySqlHelper.cs带MySql.Data.dll下载
MySqlHelper.cs代码如下: using System; using System.Collections.Generic; using System.Linq; using System. ...
- Linux的学习--使用PuTTY
交代一下背景,在笔记本上装虚拟机,在虚拟机里装了Ubuntu系统,但用着一直感觉很卡.刚好同学有旧的不用的笔记本,就拿来装了一个Ubuntu. 想要从我的笔记本上控制Ubuntu系统,于是就找到了pu ...
- Unity3D 中的三个Update()方法
MonoBehaviour.Update 更新 当MonoBehaviour启用时,其Update在每一帧被调用. MonoBehaviour.FixedUpdate 固定更新 当Mo ...
- Anliven - 你的学习为何如此低效?!
拖延 适时学习的本质就是营造机会,具有强烈的时效性,而拖延能够毁灭所有机会! 一个得不到执行的完美计划,比不上一次仓促的执行! 盲目 缺少有效的策略和方法,没有弄清基本的问题(需求--->性质- ...