Home arrow PHP arrow Page 4 - Working with Reflected Properties with the Reflection API

Getting all the properties of the User class - PHP

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

As I said in the preceding segment, getting all of the properties defined by the previous “User” class is really a breeze. The “User” class that you saw before defines only three private properties, but anyway it’s worthwhile to code an example, so you can see how easy is to accomplish this task.

Having said that, here’s a script that retrieves in one go all of the properties declared by “User”:

// 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 properties of reflected class

print_r($reflector->getProperties()); // displays Array ( [0] => ReflectionProperty Object ( [name] => id [class] => User ) [1] => ReflectionProperty Object ( [name] => fname [class] => User ) [2] => ReflectionProperty Object ( [name] => lname [class] => User ) [3] => ReflectionProperty Object ( [name] => email [class] => User ) )

 

There you have it. Once a reflector object has been created, fetching all of the properties declared by the “User” class is reduced to calling the “getProperties()”reflection method and nothing else. What else can you ask for?

Again, I’d like to clarify that the reflection API permits you to handle class properties in many other helpful and creative ways. However, in this series I only intended to introduce you to using the API’s most relevant methods in a few concrete cases. Learning the remaining ones will be left completely up to you.  

Final thoughts

In this fourth part of the series, I demonstrated with a few basic examples how to use the PHP reflection API to retrieve useful information about the properties declared by a specific class. It’s fair to mention, though, that reflection allows you to do much more with properties of a class, aside from determining their visibility or knowing the values assigned to them. At the risk of being repetitive, the best place to get a full reference of reflection methods is the official PHP web site, so go ahead and take a look at it. It’s really worthwhile, trust me.

Moving forward, in the upcoming tutorial I’m going to explore other useful reflection methods, which will allow you to get more detailed information about the properties of a class, such as if they’re static or even whether or not a specified property exists.

Here’s my recommendation: 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: