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

Retrieving 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.

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

One of the most useful features of the reflection API is its ability to retrieve information about the methods defined by a selected class, to say nothing of its manipulation ability. And by manipulation, I mean the capability to invoke a reflected method with its incoming arguments.

To elaborate on this concept, say that a script needs to check whether the "getFirstName()" method of the previous "User" class is public (which is true), and then call it from client code. Performing these tasks would be as easy coding the following snippet: 

// 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 a ReflectionMethod object

$method = $reflector->getMethod('getFirstName');

 

 

 

// check to see if the reflected method is public, protected or private

if ($method->isPublic())

{

    echo 'The method is public'; // displays 'The method is public'

   // invoke the reflected method

   echo $method->invoke($user); // displays 'Alejandro'   

}

elseif ($method->isProtected())

{

    echo 'The method is protected';

}

elseif ($method->isPrivate())

{

    echo 'The method is private';

}

elseif ($method->isStatic())

{

    echo 'The method is static';

}

Undoubtedly, a few interesting things are happening here. The above script uses a brand new reflection method called "getMethod()" to retrieve the "getFirstName()" method from the "User" class. This process returns an object of type ReflectionMethod, which can be used afterward for checking the visibility of "getFirstName()" and for calling it directly from client code.

This one example should give you an excellent idea of the advantages in using reflection in PHP, and its possible applications in real-world environments. 

Having explained how to inspect and invoke a determined method within a reflected class, the last thing that I'm going to cover in this tutorial, will consist of showing how to use reflection to retrieve all of the public methods defined by that class.

Now, to learn the full details of this process, click on the link below and read the section to come.



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

blog comments powered by Disqus
   

PHP ARTICLES

- 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...
- PHP: Building Concrete Validators
- Sanitizing Input with PHP
- Executing Shell Commands with PHP
- Handling File Data with PHP
- File Security and Resources with PHP
- ArrayObject PHP Class Examples
- ArrayObject PHP Class: An Introduction
- Getting File System Data with PHP
- PHP Tools for Working with the File and Oper...
- Working with the File and Operating System w...
- PHP Proxy Patterns: Completing a Blog


© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 7 - Follow our Sitemap

Dev Shed Tutorial Topics: