Home arrow PHP arrow Getting More Info on Reflected Properties with the Reflection API

Getting More Info on Reflected Properties with the Reflection API

In this fifth part of the series you will learn a couple of additional methods included with the PHP reflection API. The first one will allow you to check the existence of a specified class property, and the second one will permit you to retrieve all of the static properties defined by that class at once.

TABLE OF CONTENTS:
  1. Getting More Info on Reflected Properties with the Reflection API
  2. Review: getting basic information about class properties with reflection
  3. Checking for a class property's existence with the hasProperty() reflection method
  4. Retrieving all of the static properties of a class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
March 22, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Frankly speaking, the powerful reflection library that comes included with PHP 5 is one of those features that you, as a web developer, can’t live without once you’ve discovered its real functionality. While is fair to say that in the old days of PHP 4, the language already provided some native functions that allowed developers to achieve a decent level of introspection in classes and objects, the reflection API permits you to analyze the internal structure of classes and interfaces by using an intuitive object-oriented approach, or expressed in other words, you can use reflective classes that inspect other classes. What else can you ask for?

Well, feature-hungry programmers (and that includes myself, of course) will always ask for more. In its current version, however, the reflection API will let you perform a thorough reverse engineering process on your own classes, such as knowing what methods and properties they define, the level of visibility assigned to those members, and even instantiate directly classes from client code without having to explicitly write any “new” constructs.

Naturally, if you’ve been a loyal follower of this series and already went through all of the articles that precede this one, then you're already familiar with using some of the most important methods included with the reflection API. In those tutorials I coded a few simple examples that showed, for instance, how to get all of the methods declared by a class and how to determine if the methods in question were public, protected or private.

Also, I left off the last installment discussing how to retrieve all of the properties declared by the aforementioned class, along with their level of visibility, which was a straightforward process. Of course, as you might have guessed, the ability to manipulate class properties via reflection doesn’t stop here. To prove this, in the lines to come I’m going to demonstrate how to determine if a specified property exists within a class, and whether or not the class defines any static properties.

So, now that you know what to expect from this part of the series, it’s time to explore a few more handy methods bundled with the PHP reflection API. Let’s jump in!



 
 
>>> 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 11 - Follow our Sitemap

Dev Shed Tutorial Topics: