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

The getStartLine(), getEndLine() and getFileName() 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

Not surprisingly, the reflection API has the ability to get information about a class that has to do specifically with its location in the file system. For instance, it’s possible to know exactly where the definition of the class begins and ends within its containing file and the name of the file in question.

To clarify this concept, pay attention to the following code sample, which performs these tasks by using three new reflective methods: 

 

// 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 starting line number of reflected class

echo $reflector->getStartLine(); // displays '15'

// get ending line number of reflected class

echo $reflector->getEndLine(); // displays '87'

// get filename of reflected class

echo $reflector->getFileName(); // displays 'C:pathtoreflection_apireflection_api.php' 

Definitely, the starting and ending lines of a class within a file aren't the most useful things to know, but the “getStartLine()” and “getEndLine()” methods make this process a no-brainer. On the other hand, the “getFileName()” does good work returning the name of the file that included the reflected class.

And if all of these introspection capabilities still don’t convince you to use the reflection API, below is an example that shows how to use a reflector object to get the name of the interface implemented by the sample “User” class:  

// get name of implemented interfaces

print_r($reflector->getInterfaceNames()); // displays Array ( [0] => Identifier ) 

// get associative array of implemented interfaces

print_r($reflector->getInterfaces()); // displays Array ( [0] => Identifier ) Array ( [Identifier] => ReflectionClass Object ( [name] => Identifier ) )

There you have it. By using only two methods, it’s possible to determine what interfaces are implemented by a reflected class. Considering that “getInterfaces()” returns a new ReflectionClass object, it’s easy to iterate over all of the interfaces implemented by a class in the following way:

$reflector = new ReflectionClass('User');

foreach ($reflector->getInterfaces() as $interface)

{

    echo $interface->getName() . '<br />';

}

And with this final example, we’ve come to the end of this second chapter of the series. As always, feel free to edit all of the code samples shown in this tutorial, a process that surely will give you a more intimate knowledge of reflection in PHP 5.

Final thoughts

In this second episode of the series, I explored a few handy methods packaged with the PHP reflection API, which allowed you to retrieve miscellaneous information about a class, including its name and containing file, as well as its starting and ending lines.

In addition, the use of the “getInterfaceNames()” and “getInterfaces()” methods permitted us to inspect the interfaces implemented by a class. This can be useful in situations where client code needs to know if an object honors a determined interface contract.

In the upcoming tutorial, things will become even more interesting. I’m going to discuss other helpful methods, which will be used to obtain all sorts of rich information about the methods defined by a reflected class.

My suggestion is simple: don’t miss the next part!



 
 
>>> 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: