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

Determining if a class is abstract, final and an interface implementer - 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 with the other reflection processes covered in this and previous tutorials of the series, checking if a class has been defined abstract and final (or both) is just a matter of calling the proper reflective method and nothing else. It’s that easy, really.

However, if you’re anything like me, you want to see a practical example that shows how to accomplish these tasks. Below I coded a simple script that verifies not only if the earlier “User” class is abstract and final, but checks to see if it’s an implementer of the “Identifier” interface, which in this case turns out to be true.

Here’s the script in question:

// create instance of 'User' class

$user = new User();

 

 

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

 

 

$reflector = new ReflectionClass('User');

 

 

// check if the reflected class is abstract

if ($reflector->isAbstract())

{

    echo 'Reflected class is abstract'; // displays 'Reflected class is not abstract'

}

else

{

    echo 'Reflected class is not abstract';

}

 

 

// check if the reflected class is final

if ($reflector->isFinal())

{

    echo 'Reflected class is final'; // displays 'Reflected class is not final'

}

else

{

    echo 'Reflected class is not final';

}

 

 

// check if the reflected class is an implementer of a specified interface

if ($reflector->implementsInterface('Identifier'))

{

    echo 'Reflected class is an implementer of the specified interface'; // displays 'Reflected class is an implementer of the specified interface'

}

else

{

    echo 'class is not an implementer of the specified interface';

}

Definitely, the above script speaks for itself, as it invokes three brand new reflection methods called “isAbstract(),” “isFinal()” and “implementsInterface()” respectively, to check if the “User” class is abstract and final, and also if it implements the “Identifier” interface.

To be frank, understanding the underlying logic of these methods is far from rocket science, so it’s time to explore other handy methods offered by the reflection API. Therefore, in the following segment I’m going to discuss the use of the “isInstantiable()” method, which as its name suggests, allows you to know if a class can be instantiated.

This topic will be the conclusion of this sixth installment of the series, so please click on the link that appears below and read the lines to come.   



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