Home arrow PHP arrow Page 4 - Reflecting Abstract Classes and Interface Implementers with the Reflection API

Checking if a class can be instantiated - PHP

In this sixth part of a seven-part series, I explain how to use the power of reflection to find out if a class has been defined abstract and final, and if it implements a specified interface. Performing all of these tasks is a straightforward process, so you shouldn’t have major problems using these reflection methods within your own PHP applications.

TABLE OF CONTENTS:
  1. Reflecting Abstract Classes and Interface Implementers with the Reflection API
  2. Review: the hasProperty() and getStaticProperties() reflection methods
  3. Determining if a class is abstract, final and an interface implementer
  4. Checking if a class can be instantiated
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 29, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

As I promised in the section that you just read, I’d like to finish this tutorial by showing you how to use reflection for verifying whether or not a specified class can be instantiated from client code. To achieve this, the reflection API provides the handy “isInstantiable()” method, which can be used in the following way:

// create instance of 'User' class

$user = new User();

 

 

// create instance of Reflection class and pass in 'User' class as argument

 

 

$reflector = new ReflectionClass('User');

 

 

// check if the reflected class can be instantiated

if ($reflector->isInstantiable())

{

    echo 'Reflected class is instantiable'; // displays 'Reflected class is instantiable'

}

else

{

    echo 'Reflected class is not instantiable';

}

Since the sample “User” class can indeed be instantiated, in the above checking block the “isInstantiable()” method will return a value of TRUE. Nonetheless, if you use it with an abstract class or with one that declares its constructor protected or private, the method will return FALSE.

While checking if it’s possible to create instances of a class seems to be at first sight a rather pointless task, this is far from being true. This reflection process is often found in frameworks that implement some sort of dispatcher mechanism, and in some dependency injection containers as well. So, if you need to inspect classes an runtime with an acceptable level of automation, the methods offered by the reflection API are definitely an option worth looking at.

Final thoughts

In this sixth part of the series, I illustrated with some easy-to-grasp code samples how to use the power of reflection to know if a class has been defined abstract and final, and if it implements a specified interface. As you saw for yourself, performing all of these tasks was a straightforward process, meaning that you shouldn’t have major problems using the previous reflection methods within your own PHP applications.

In the last episode, the focus will be on how to take advantage of reflection to verify whether a class is native or user-defined.

Don’t miss the final article!



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