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

Getting all the public methods of a reflected 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.

  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



As I said in the section that you just read, the reflection API makes it really easy to get all of the public, protected and private methods of a class. For the sake of brevity, in this case I'm going to show how to retrieve only the public ones, but obviously a similar approach can be used to get those with a stronger visibility.

Now that I've clarified that, please check the following code sample. It uses another reflection method, called "getMethods()," which returns an array containing the public methods of the previous "User" class:  

// 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 all public methods of reflected class

print_r($reflector->getMethods(ReflectionMethod::IS_PUBLIC)); // displays Array ( [0] => ReflectionMethod Object ( [name] => __construct [class] => User ) [1] => ReflectionMethod Object ( [name] => setId [class] => User ) [2] => ReflectionMethod Object ( [name] => getId [class] => User ) [3] => ReflectionMethod Object ( [name] => setFirstName [class] => User ) [4] => ReflectionMethod Object ( [name] => getFirstName [class] => User ) [5] => ReflectionMethod Object ( [name] => setLastName [class] => User ) [6] => ReflectionMethod Object ( [name] => getLastName [class] => User ) [7] => ReflectionMethod Object ( [name] => setEmail [class] => User ) [8] => ReflectionMethod Object ( [name] => getEmail [class] => User ) )

As you can see, the "IS_PUBLIC" constant of the ReflectionMethod class is inputted into "getMethods()" to get all of the public methods of "User." This outputs an array that naturally can be traversed by using a regular "foreach" construct.

In a similar way, I could have passed to "getMethods()" the "IS_PROTECTED" and "IS_PRIVATE" constants to retrieve the protected and private methods respectively, but since this process is fairly straightforward, it will be left as homework for you.

And with this last example, we've come to the end of this third part of the series. As usual, feel free to tweak all of the code samples included in this tutorial. Doing this will hopefully will arm yourself with a more solid background in using the PHP reflection API.

Final thoughts

That's all for now. In this third installment of the series you learned how to take advantage of the functionality provided by the PHP reflection API to retrieve useful information about the methods defined by a reflected class.

In the examples shown, the power of reflection was used not only to get the names of those methods, but for determining their level of visibility. Of course, it's feasible to manipulate reflected methods in many more useful ways, but this will be left as a practical exercise for you.

In the forthcoming part, I'm going to teach you how to use reflection to work with properties of a class, which will be a very juicy topic, trust me. Don't miss the upcoming article!

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