Home arrow PHP arrow Working with Reflected Properties with the Reflection API

Working with Reflected Properties with the Reflection API

In this fourth part of a seven-part series, I demonstrate how to use the PHP reflection API to retrieve useful information about the properties declared by a specific class. Reflection allows you to do more with properties of a class than determine their visibility or learn the values assigned to them, but that's what we'll be covering here.

TABLE OF CONTENTS:
  1. Working with Reflected Properties with the Reflection API
  2. Review: working with reflected methods
  3. Manipulating properties of a reflected class
  4. Getting all the properties of the User class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
March 15, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

In case you’re not aware of it, among the many enhancements and helpful features that were added to PHP 5, there’s one that has been unfairly overlooked by many developers. This might be because at first glance it doesn't seem to be as useful as the others. Yes, as this article’s title suggests, I’m talking about the reflection API, a powerful set of methods that permits you to perform an in-depth reverse engineering process on both classes and interfaces, in this way expanding the language’s introspective capabilities.

Fortunately, the API has progressively come out of the shadows of indifference, and nowadays its popularity has grown considerably. It's being used in a huge variety of scenarios and situations, ranging from creating low-level dispatcher classes in MVC-based frameworks, to building dependency injection containers.

Of course, it's possible that you still haven’t had the chance to explore this API in depth and discover what it can do for you. If that’s the case, in the course of this article series, I’m going to show you how to work with its most relevant methods, so you can start taking advantage of reflection within your own web applications.

I left off the previous tutorial explaining how to use the reflection API to get relevant information about the methods defined by a sample class, including the ability to learn if the methods in question were declared public, protected or private. However, the power of reflection doesn’t stop here; it allows you to analyze class properties with a similar level of introspection.

If this topic has caught your attention, in the following lines I’m going to code some friendly examples to demonstrate the functionality of the PHP reflection API when it comes to inspecting properties of a class.

Now, let’s get rid of the dull theory and learn more about using reflection in PHP 5. Let’s get going!



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

blog comments powered by Disqus
   

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

 



© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap

Dev Shed Tutorial Topics: