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

Review: the hasProperty() and getStaticProperties() reflection methods - 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

In the previous article, as I mentioned, I explained how to use the “hasProperty()” and “getStaticProperties()” reflection methods with a sample class. Below I reintroduced the source code corresponding to these examples, starting with the definitions of the class itself and its associated interface. Here they are:

interface Identifier

{

    public function setId($id);

   

    public function getId();

    

}

 

 

/**

* A sample user class

*

* @param  id fname lname email

*/

class User implements Identifier

{

    private $id = NULL;

    private $fname = 'Alejandro';

    private $lname = 'Gervasio';

    private $email = 'alejandro@domain.com';

    const HEADING = 'Using the Reflection API in PHP 5';

   

    // constructor (not implemented)

    public function __construct(){}

   

    //setter for id property

    public function setId($id)

    {

        if (!is_numeric($id))

        {

            throw new Exception('ID must be a numeric value');

        }

        $this->id = $id;

    }

   

    // getter for id property

    public function getId()

    {

        return $this->id;

    }

       

    // setter for fname property

    public function setFirstName($fname)

    {

        if (empty($fname) OR !is_string($fname))

        {

            throw new Exception('First name must be a non-empty string.');

        }

        $this->fname = $fname;

    }

   

    // getter for fname property

    public function getFirstName()

    {

        return $this->fname;

    }

   

    // setter for lname property

    public function setLastName($lname)

    {

        if (empty($fname) OR !is_string($fname))

        {

            throw new Exception('Last name must be a non-empty string.');

        }

        $this->lname = $lname; 

    }

   

    // getter for lname property

    public function getLastName()

    {

        return $this->lname;

    }

   

    // setter for email property

    public function setEmail($email)

    {

        if (empty($email) OR !is_string($email) OR strpos($email, '@') === FALSE)

        {

            throw new Exception('Email must be a well-formatted email address.');

        }

        $this->email = $email; 

    }

   

    // getter for email property

    public function getEmail()

    {

        return $this->email;

    }         

}

Since the “Identifier” interface and its implementing “User” class have been coded only for demonstration purposes, explaining how they function (again) would be a waste of time. Instead, you should look at the following code fragment, which shows how to use the aforementioned “hasProperty()” and “getStaticProperties()” methods, first for checking the existence of a specific property, and then for getting all of the static properties defined by this class:

// 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 has a specified property

if ($reflector->hasProperty('lname'))

{

    echo 'Property found in reflected class'; // displays 'Property found in reflected class'

}

else

{

    echo 'Property not found in reflected class';

}

 

 

// get static properties of reflected class

print_r($reflector->getStaticProperties()); // displays nothing

Undeniably, getting information about the properties of a class is a no-brainer process, thanks to the numerous methods included with the reflection API. Again, it’s fair to stress here that the API offers many other handy methods that will let you analyze your classes and interfaces down to their bare bones, so if you need to perform a particular introspection task that hasn’t been covered in this series, make sure to check the API’s official online documentation.

So far, so good. At this stage, you've learned how to obtain relevant data about the properties of a class, so now I’m going to discuss using reflection to know if a class has been declared abstract and final.

These subjects will be covered in the section to come. To get there, simply click on the link below and read the next few lines.     



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