Home arrow PHP arrow Page 2 - The Reflection API: Working with Reflected Methods

Review: using the PHP reflection API to retrieve information on a class - PHP

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.

TABLE OF CONTENTS:
  1. The Reflection API: Working with Reflected Methods
  2. Review: using the PHP reflection API to retrieve information on a class
  3. Retrieving methods of a reflected class
  4. Getting all the public methods of a reflected class
By: Alejandro Gervasio
Rating: starstarstarstarstar / 2
March 08, 2010

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

Just in case you haven't had the chance to look at the preceding article of the series, where I discussed using the reflection API to get miscellaneous information about an example class, I'm going to include a brief view. Below I reintroduced the definition of this class and its associated interface, along with a script that pulls out mixed data from these two structures.

Here's the source code of the interface and its implementing class:

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;

    }         

}

As you can see, the previous "User" class and the pertinent "Identifier" interface are nothing but simple examples for putting the reflection API to work. The following code fragment shows how to use the API to get general information about that class by using a few straightforward methods. Check it out:

// create instance of 'User' class

$user = new User();

 

 

 

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

$reflector = new ReflectionClass('User');

 

 

 

// get default properties in reflected class

print_r($reflector->getDefaultProperties()); // displays Array ( [id] => [fname] => Alejandro [lname] => Gervasio [email] => alejandro@domain.com )

 

 

 

// get doc comments in reflected class

print_r($reflector->getDocComment()); // displays /** * A sample user class * * @param id fname lname email */

// get starting line number of reflected class

echo $reflector->getStartLine(); // displays '15'

 

 

 

// get ending line number of reflected class

echo $reflector->getEndLine(); // displays '87'

 

 

 

// get filename of reflected class

echo $reflector->getFileName(); // displays 'C:pathtoreflection_apireflection_api.php' 

 

 

 

// get name of implemented interfaces

print_r($reflector->getInterfaceNames()); // displays Array ( [0] => Identifier ) 

 

 

 

// get associative array of implemented interfaces

print_r($reflector->getInterfaces()); // displays Array ( [0] => Identifier ) Array ( [Identifier] => ReflectionClass Object ( [name] => Identifier ) )

Retrieving miscellaneous data about the earlier "User" class is a simple process, thanks to the use of some intuitive reflection methods. In the above example, those methods are utilized to determine the starting and ending lines of the class, and the name of its containing file.

As a bonus, the API is finally employed to get the names of the interfaces implemented by "User," which in this case is "Identifier," whose corresponding definition was shown before.

Okay, now that you know how to use the power of reflection to analyze certain internal aspects of a class, I'm going to teach you how to get relevant information about the methods defined by the "User" class. This includes the ability to check whether these methods are public, protected or private.

To learn the full details of this reflection process, click on the link below and read the section 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: