Home arrow PHP arrow Page 2 - Getting Information on a Reflected Class with the Reflection API

Review: using the PHP 5 reflection API - PHP

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.

  1. Getting Information on a Reflected Class with the Reflection API
  2. Review: using the PHP 5 reflection API
  3. The getDefaultProperties() and getDocComment() reflection methods
  4. The getStartLine(), getEndLine() and getFileName() methods
By: Alejandro Gervasio
Rating: starstarstarstarstar / 1
March 03, 2010

print this article



As usual, before I proceed to demonstrate how to use other useful methods included with the PHP reflection API, it’d be convenient to take a quick look at the code samples developed in the previous article.

In that part of the series I showed how to get the name and the values assigned to the constant declared by a sample class. The class in question was a really basic one, and implemented an interface called “Identifier.”

The definitions of this interface and the implementing class are shown below:

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;



From the above code fragment, it’s clear to see that the purpose of coding the “Identifier” interface and its implementing “User” class is only to have two sample structures that can be easily analyzed internally via reflection.

Things start to look more interesting, though, when these example entities are reverse-engineered through some reflective methods. The following code snippet shows how to get the name of a reflected class and information about the constants that it declares in a few simple steps:

// 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 name of reflected class

echo $reflector->getName(); // displays 'User'

// get constant in reflected class

echo $reflector->getConstant('HEADING'); // displays 'Using the Reflection API in PHP 5'

// get an array of constants in reflected class

print_r($reflector->getConstants()); // displays Array ( [HEADING] => Using the Reflection API in PHP 5 )

See how easy it is to collect information on a selected class via the reflection API? I guess you do! In this case, the information retrieved is somewhat trivial, but it demonstrates how powerful the API can be for stripping classes and interfaces down to their bare bones by means of an object-oriented approach.

Of course, if you’re like me, you'll typically want to know more about a class or interface, aside from retrieving its name and its declared constants. Fortunately, the reflection API provides some other methods that allow you to fetch mixed class information in a simple manner.

In the following section I’m going to teach you how to retrieve the set of default properties declared by the earlier “User” class, along with the data embedded into its doc block.

To learn more about how this will be done, click on the link below and read the segment to come.

>>> More PHP Articles          >>> More By Alejandro Gervasio

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


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