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

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


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

Dev Shed Tutorial Topics: