views:

99

answers:

1

Hello,

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?
+5  A: 

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, many of the newer additions to PHP > 5.2, are poorly documented. Just look at the SPL. Same thing. That doesn't mean you cannot use it though. PHP used to have a great documentation, but I guess the maintainers just don't find the time nowadays. Feel free to offer your help.

If you want to learn about any undocumented packages in PHP, just google for it. 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 ]
  }
}
Gordon
@Gordon: hmmm that is helpful answer +1, thanks :)
Sarfraz