Introducing the Reflection API in PHP 5

Read full article | No Comments

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.

Even though PHP is intrinsically a weakly-typed language, as it doesnt 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 its 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 languages introspective abilities to a far more mature stage. Whats 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 its 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 Im 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. Itll be an instructive experience, trust me!

Getting Information on a Reflected Class with the Reflection API

Read full article | No Comments

In this second part of a series, I explore some handy methods of the PHP Reflection API. They allow you to retrieve miscellaneous information about a class, including its name and containing file, as well as its starting and ending lines.
Read More ……

The Reflection API: Working with Reflected Methods

Read full article | No Comments

This third part of a seven-part series explores the methods of the PHP Reflection API. You will learn how to take advantage of their functionality to retrieve useful information about the methods defined by a reflected class.
Read More ……

Defining an Abstract Class with Restrictive Constructors in PHP 5

Read full article | No Comments

In this second part of a four-part series, I rebuild the example application developed in the previous tutorial. As you’ll recall, it used a protected constructor to prevent the instantiation of the base array iterator class. In this case, a better result will be achieved by declaring the iterator abstract; still, the example demonstrates a simple utilization of a restrictive constructor in a concrete situation.
Read More ……

Implementing the Data Mapper Design Pattern in PHP 5

Read full article | No Comments

In this first part of a five-part series, I introduce you to implementing the Data Mapper design pattern in PHP 5. This pattern attempts to solve the issue of the strong coupling that often exists between the domain objects present in an application and the underlying persistence mechanism. This benefit comes at a cost, since data mappers add a new layer of complexity. Living with this minor trade-off is quite worthwhile, though, as you’ll see.
Read More ……

The Iterator, Countable and ArrayAccess SPL Interfaces

Read full article | No Comments

In this first part of a short series, I’m going to introduce you to using the Iterator, Countable and ArrayAccess interfaces that come with the Standard PHP Library. You’ll see how to implement some of the methods declared by the Iterator interface within a class that manipulates MySQL result sets via an intuitive API.
Read More ……

Working with Reflected Properties with the Reflection API

Read full article | 1 Comment

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.
Read More ……

Building a Data Access Layer for the Data Mapper Design Pattern

Read full article | No Comments

In this second part of a five-part series I go one step further in the implementation of the Data Mapper design pattern in PHP 5. Specifically, I build a simple MySQL abstraction class which performs a few common tasks. These include connecting to the database server and running hard-coded queries, fetching database rows and so forth.
Read More ……

Building a Singleton Database with Restrictive Constructors in PHP 5

Read full article | No Comments

In this third installment of a four-part series, I show you that a private constructor can be truly helpful when strictly implementing the Singleton design pattern. In this case, the pattern will be applied within a class that behaves like a simple MySQL abstraction layer, but the same concept can be extended to other classes.
Read More ……

Triggering Cascading Updates and Deletions with Foreign Key Constraints

Read full article | No Comments

Welcome to the final installment of a series on working with foreign key constraints in MySQL. With a respectable number of code samples, this series walks you through the basics of using foreign key constraints in MySQL tables and teaches you how to use them specifically for maintaining the integrity of relational databases.
Read More ……