Home arrow PHP arrow Introducing the Reflection API in PHP 5

Introducing the Reflection API in PHP 5

In this first part of a series, we'll begin exploring some of the methods included with the Reflection API bundled with PHP 5. The interface allows developers to collect relevant information about a reflected class, including its name, its declared constants and properties, in a extremely straightforward way.

TABLE OF CONTENTS:
  1. Introducing the Reflection API in PHP 5
  2. Building a sample interface and a basic class
  3. Finding the name of a reflected class
  4. Finding the constants defined by a reflected class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 02, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Even though PHP is intrinsically a weakly-typed language, as it doesn’t internally impose any constraints on the types of values that can be held by a given variable (with the exception of type hinting and type casting), its introspective capabilities are quite impressive indeed. Native old functions like “is_int(),” “is_string()” and “is_float()” among others, which have been used for years, gave programmers the ability to check at runtime if the value assigned to a variable honors a specified type.

While it’s fair to say that these and other functions play a relevant role within the introspective facet of the language, the truth is that they fall short and become useless quickly, especially when it comes to analyzing the internal structure of classes and interfaces. Frankly speaking, PHP 4 already included a set of functions that allowed developers to inspect classes at a limited level, and they continue to be widely used in programs today. Unfortunately, with the introduction of a highly-enhanced object model in PHP 5, the limitation imposed by these functions became even more evident.

The happy side of this story is that, along with its most popular object-oriented bells and whistles, PHP 5 was also packaged with a powerful reflection API. This API takes the language’s introspective abilities to a far more mature stage. What’s more, it includes some convenient methods that permit developers to dissect both classes and interfaces down to their bare bones, which can be much more useful than you might think.

As with many other features of PHP 5, unquestionably the best place to get an intimate background in its reflection API is the official documentation. Even so, for many lazy programmers (like me), sometimes it’s good to have at hand an additional guide that quickly shows how to use the most relevant methods of the API in question.

With that idea in mind, in this group of tutorials I’m going to attempt to provide you with a variety of simple code samples that hopefully will get you started using the PHP 5 reflection API -- without having to scratch your head trying to figure out how to work with its methods.

Are you ready to start digging deeper into the reflection programming interface provided by PHP 5? Then click on the link below and begin reading the next few lines. It’ll be an instructive experience, trust me!



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