Home arrow PHP arrow Page 3 - Getting Information on a Reflected Class with the Reflection API

The getDefaultProperties() and getDocComment() reflection methods - PHP

In this second part of a series, I explore some handy methods of the PHP Reflection API. They allow you to retrieve miscellaneous information about a class, including its name and containing file, as well as its starting and ending lines.

TABLE OF CONTENTS:
  1. Getting Information on a Reflected Class with the Reflection API
  2. Review: using the PHP 5 reflection API
  3. The getDefaultProperties() and getDocComment() reflection methods
  4. The getStartLine(), getEndLine() and getFileName() methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
March 03, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As you learned in the previous section, once a reflector object has been tied to a specific class, it’s very easy to collect information about its internal structure. So far, I've showed how to use reflection to get the name of a class, along with its constants, but it’s possible to find out much more about it.

To demonstrate this, below I wrote a simple script that shows how to get the default properties declared by the “User” class, along with the data included within its doc block. The script that does that looks like this: 

// create instance of 'User' class

$user = new User();

// create instance of Reflection class and pass in 'User' class as argument

$reflector = new ReflectionClass('User');

// get default properties in reflected class

print_r($reflector->getDefaultProperties()); // displays Array ( [id] => [fname] => Alejandro [lname] => Gervasio [email] => alejandro@domain.com )

// get doc comments in reflected class

print_r($reflector->getDocComment()); // displays /** * A sample user class * * @param id fname lname email */

That was really easy to achieve, wasn’t it? By calling the new “getDefaultProperties()” and “getDocComment()” methods, it’s feasible to get the default properties declared by the “User” class along with the information embedded in its document block. As you can see, both methods return data in the form of arrays, which can be easily dumped to the browser or eventually stored in variables for further processing.

Now that you've learned how to use a couple of additional methods provided by the reflection API, it’s time to explore a few others. However, before I do that, I’d like to stress one important thing with reference to the previous example: while “getDefaultProperties()” does a decent job returning the set of default properties declared by a reflected class, in many situations it’s desirable to know their level of visibility as well.

Does this mean that the reflection API isn’t capable of doing such a thing? Fear not; it can do this by using a specialized class called “ReflectionProperty,” which will be properly analyzed in a future article. For the moment though, be patient and read the following section. In it I’m going to discuss how to use the API for collecting information about the physical location of the “User” class inside the file system.

Now, click on the link below and keep reading, please. 



 
 
>>> More PHP Articles          >>> More By Alejandro Gervasio
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PHP ARTICLES

- Hackers Compromise PHP Sites to Launch Attac...
- Red Hat, Zend Form OpenShift PaaS Alliance
- PHP IDE News
- BCD, Zend Extend PHP Partnership
- PHP FAQ Highlight
- PHP Creator Didn't Set Out to Create a Langu...
- PHP Trends Revealed in Zend Study
- PHP: Best Methods for Running Scheduled Jobs
- PHP Array Functions: array_change_key_case
- PHP array_combine Function
- PHP array_chunk Function
- PHP Closures as View Helpers: Lazy-Loading F...
- Using PHP Closures as View Helpers
- PHP File and Operating System Program Execut...
- PHP: Effects of Wrapping Code in Class Const...

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: