advertisement
Premier Club Log In/Registration
  Include Code  Search Tips
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   SKILLBUILDING  |   TIP BANK  |   SOURCEBANK  |   FORUMS  |   NEWSLETTERS
Browse DevX
Download the sample code for this article.
Partners & Affiliates
advertisement
advertisement
advertisement
Rate this item | 0 users have rated this item.
Email this articleEmail this article
 
Taking Advantage of the New Reflection API in PHP5
Discover how to use the Reflection API to extract information about PHP classes and execute class methods.  

advertisement
f you haven't yet upgraded from PHP4 to PHP5, this article will give you a serious reason to do so: PHP5 now supports reflection through the Reflection API, which you can use to examine variables, interfaces, functions, methods, parameters, classes—and more. The Reflection API is generous; it offers a large number of classes and methods that you can use to accomplish reflection tasks.


To get a sense of the the theoretical aspect of the Reflection API, you'll see a brief description of the Reflection API classes and methods, illustrated by short examples. Later, you'll see two real-world application examples in PHP that make run-time decisions about which methods to call and auto-generate well-formatted HTML documentation for your PHP classes.

Reflection API Classes and Methods
 
Figure 1. Reflection API Classes: Here are the relationships between the Reflection API classes and methods.
As a quick overview of the Reflection API's capabilities, here's a diagram (see Figure 1) that presents the Reflection API classes/methods and the relationships between them. Notice that all the Reflection API classes implement the Reflector interface except for Reflection, ReflectionException, and ReflectionFunctionAbstract. All exportable Reflection classes implement the Reflector interface.

The ReflectionClass
You can think of the ReflectionClass class as the main class of the Reflection API. You use it to apply reflection over other classes, extracting timely information about all class components. I'll show you a few examples of using ReflectionClass methods and then just list the calls and descriptions for the rest, because they're all similar.

  • public void construct(string name)—This is the ReflectionClass constructor.
  • public string getName()—This method returns the inspected class name.
   // EXAMPLE
   $class = new ReflectionClass('TestClass');
   echo "The class name: ".$class->getName();
  • public ReflectionMethod getConstructor()—This method returns a ReflectionMethod object that represents the introspected class constructor.
   // EXAMPLE
   $class = new ReflectionClass('TestClass');
   $constructor = $class->getConstructor();
   echo $constructor;
  • public ReflectionMethod getMethod(string name)—This method returns a ReflectionMethod object representing the method specified by the name parameter.
   // EXAMPLE
   $class = new ReflectionClass('TestClass');
   $method = $class->getMethod('testMethod_1');
   echo $method;
  • public ReflectionMethod[] getMethods()—Returns an array of RelectionMethod objects containing all the introspected class methods.
   // EXAMPLE
   $class = new ReflectionClass('TestClass');
   $methods = $class->getMethods();
   foreach($methods as $in)
      { echo $in; }
In addition to the methods already listed, the class exposes similar methods that you use in the same way: You create a ReflectionClass instance, passing the name of the class you want to inspect as a parameter, and then use that instance to call the methods below:

  • public ReflectionProperty getProperty(string name)
  • public ReflectionProperty[] getProperties()
  • public mixed getConstant(string name)
  • public array getConstants()
  • public ReflectionClass[] getInterfaces()
The objects you retrieve using the preceding methods give you access to "deeper" levels of information in the class, as described in the next section.

  Next Page: Useful Reflection Classes and Methods


Page 1: IntroductionPage 3: Example 1: A Simple Reflection Application
Page 2: Useful Reflection Classes and MethodsPage 4: Example 2: Generating PHP Documentation with Reflection
Untitled
advertisement
Advertising Info  |   Member Services  |   Permissions  |   Contact Us  |   Help  |   Feedback  |   Site Map  |   Network Map  |   About


JupiterOnlineMedia

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info


Legal Notices, Licensing, Reprints, & Permissions, Privacy Policy.

Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

Solutions
Whitepapers and eBooks
Intel PDF: Virtualization Delivers Data Center Efficiency
Intel eBook: Managing the Evolving Data Center
Microsoft Article: BitLocker Brings Encryption to Windows Server 2008
Symantec eBook: The Guide to E-Mail Archiving and Management
Microsoft Article: RODCs Transform Branch Office Security
Go Parallel Article: James Reinders on the Intel Parallel Studio Beta Program
Avaya Article: Advancing the State of the Art in Customer Service
Adobe Acrobat Connect Pro: Web Conferencing and eLearning Whitepapers
Avaya Article: Avaya AE Services Provide Rapid Telephony Integration with Facebook
Go Parallel Article: Getting Started with TBB on Windows
HP eBook: Storage Networking , Part 1
MORE WHITEPAPERS, EBOOKS, AND ARTICLES
Webcasts
Intel Seminar: Efficiencies in Hardware/Software Virtualization
HP Webcast: Disaster Recovery Planning
Go Parallel Video: Performance and Threading Tools for Game Developers
HP Video: StorageWorks EVA4400 and Oracle
HP Webcast: Storage Is Changing Fast - Be Ready or Be Left Behind
MORE WEBCASTS, PODCASTS, AND VIDEOS
Downloads and eKits
IBM TCO eKIT: Your IT Budget is Under Attack, Get in Control
IBM Energy Efficiency eKIT: Learn How to Reduce Costs
30-Day Trial: SPAMfighter Exchange Module
Red Gate Download: SQL Toolbelt and free High-Performance SQL Code eBook
Iron Speed Designer Application Generator
MORE DOWNLOADS, EKITS, AND FREE TRIALS
Tutorials and Demos
Microsoft Article: Silverlight Streaming--Free Video Hosting for All
Featured Algorithm: Intel Threading Building Blocks - parallel_reduce
HP Demo: StorageWorks EVA4400
MORE TUTORIALS, DEMOS AND STEP-BY-STEP GUIDES