PHP:反思API - 很少添加到PHP中 [英] PHP: The Reflection API - Great Addition To PHP With Little Use

查看:91
本文介绍了PHP:反思API - 很少添加到PHP中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

反思API是操作OOP东西的好东西,看起来大多数开发人员甚至没有意识到或从未使用过。



反射API声明


PHP 5附带了一个完整的反映
API,
反向工程类,接口,
函数,方法和扩展名。
另外,反射API
提供了检索功能,类和方法的文档评论
的方法。


问题:




  • 为什么大部分方法/属性几乎没有文档?您在文档中看到此消息:




此功能目前不是
文档;只有其参数列表是
可用。


在哪里可以找到其文档?




  • 是否与未来兼容,换句话说,我可以在我的MVC框架中使用吗?

  • 为什么使用次数较少,有什么问题吗?


解决方案

反思绝对是留在这里。您可以使用它,但请记住,对于简单的UseCases而言,它被称为缓慢且过度的杀伤力。使用类/对象功能包中的其中一个功能通常是更快的替代方案。



一个UseCase,其中Reflection可以方便地解析类的DocBlock中的注释。例如,PHPUnit使用注释@test来告诉PHPUnit TestRunner它应该考虑一种方法进行测试。 @covers注释将帮助它收集代码覆盖率数据。 FLOW3框架利用其AOP框架的注释



不幸的是,PHP> 5.3中的一些新增功能尚未记录下来。 请看SPL 。一样。这并不意味着你不能使用它。 Reflection API非常富有表现力,很容易从方法名称中找出。而Google通常会有关于如何使用某些扩展的博文。对于Reflection API,请查看:





和SPL








最近刚刚发现的东西很酷从5.1.2开始,您可以从命令行调用Reflection API

  $ php --rf strtotime 
函数[< internal:date> function strtotime] {

- 参数[2] {
参数#0 [< required> $ time]
参数#1 [<可选> $ now]
}
}


Reflection API is great thing out there to manipulate the OOP stuff and looks like most of the developers aren't even aware of that or have never used that.

Reflection API Claims:

PHP 5 comes with a complete reflection API that adds the ability to reverse-engineer classes, interfaces, functions, methods and extensions. Additionally, the reflection API offers ways to retrieve doc comments for functions, classes and methods.

Question:

  • Why it has almost no documentation for most of its methods/properties? You see this message there on the documentation:

This function is currently not documented; only its argument list is available.

Where to find its documentation at all?

  • Is it future-compatible, in other words, can i use in my MVC framework?
  • Why it is used less often, anything wrong with it?

解决方案

Reflection is definitely here to stay. You may use it, but keep in mind it is said to be slow and overkill for simple UseCases. Using one of the functions in the Classes/Objects function package is often the faster alternative.

A UseCase where Reflection comes in handy is when parsing for annotations in the DocBlock of a class. For instance, PHPUnit uses the annotations @test to tell the PHPUnit TestRunner that it should consider a method a test. The @covers annotation will help it collect Code Coverage data. The FLOW3 framework makes use of Annotations for their AOP framework.

Unfortunately, some of the newer additions to PHP > 5.3, are not documented yet that much. Just look at the SPL. Same thing. That doesn't mean you cannot use it though. The Reflection API is very expressive and easy to figure out from the method names. And Google often has blog posts about how to use certain extensions. For the Reflection API, check out:

and for SPL


Something cool I just discovered recently. As of 5.1.2, you can invoke the Reflection API from the command line too:

$php --rf strtotime
Function [ <internal:date> function strtotime ] {

  - Parameters [2] {
    Parameter #0 [ <required> $time ]
    Parameter #1 [ <optional> $now ]
  }
}

这篇关于PHP:反思API - 很少添加到PHP中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆