RSS Feed
Download our iPhone app
Browse DevX
Sign up for e-mail newsletters from DevX


Taking Advantage of the New Reflection API in PHP5 : Page 3

Discover how to use the Reflection API to extract information about PHP classes and execute class methods.

Example 1: A Simple Reflection Application
The examples from this article were tested on the TestClass PHP class shown in Listing 1 and available in the downloadable code.

Figure 2. The CurrencyConverter Application: The sample application converts values between currencies.
Here's an example that uses the Reflection API information from the previous section to develop a simple currency converter application. The starting point is an HTML page called index.htm that looks like the one you see in Figure 2. There's nothing special about the page itself, but here's the HTML:

    <h3>Currency converter</h3>
   <form method="get" 
   <b>Convert this amount:<br /><br /></b>
   <input type="text" value="1" name="amount"> <br /><br />
   <b> From this currency:</b><b> &nbsp;&nbsp; 
       To this currency </b> <br /><br />
   <select name="currency1" size="1">
   <option value="EUR">EUR 
   <option value="USD">USD
   <option value="GBP">GBP</select>
   <select name="currency2" size="1">
   <option value="EUR">EUR 
   <option value="USD">USD
   <option value="GBP">GBP</select>
   <input type="submit" value="Exchange">
   <input type="reset" value="Reset">
The interesting part begins after a user enters a value and selects the "from" and "to" currencies for the conversion. The dropdown lists each contain the abbreviations for three currency types: U.S. dollars (USD), Great Britain Pounds (GBP), and European Union Euros (EUR). When a user clicks the Exchange button, the form submits the data, and the server constructs a PHP class instance named CurrencyConverter, which contains six methods that accomplish all the possible conversions:

   class CurrencyConverter {
      function __construct() {}
      function EUR_USD($eur) {
         return $eur.' EUR = '.$usd.' USD ';
      function USD_EUR($usd) {
         return $usd.' USD = '.$eur.' EUR ';
      function GBP_EUR($gbp) {
         return $gbp.' GBP = '.$eur.' EUR ';
      function EUR_GBP($eur) {
         return $eur.' EUR = '.$gbp.' GBP ';
      function GBP_USD($gbp) {
         return $gbp.' GBP = '.$usd.' USD ';
      function USD_GBP($usd) {
         return $usd.' USD = '.$gbp.' GBP ';
Because the conversion method depends on the currencies selected by the user, the application must determine the appropriate method to call at runtime. You could make that decision by using a set of conditional statements or by using the call_user_func method, but Reflection in PHP 5 provides a more elegant solution. Determining the appropriate method is simple. Notice how the CurrencyConverter methods are named: EUR_USD, USD_EUR, etc. When a user selects two currencies, the console submits the selected currency abbreviations. In the PHP code, you simply need to concatenate the submitted currency strings with an intervening underscore (_) character. For example, if the user elects to convert from euros (EUR) to pounds (GBP), the application calls the EUR_GBP method. The application actually makes the call using reflection. You can see the entire process in the code for the currencyconverter.php page:

   //include the CurrencyConverter class
   //Get the amount to be converted
   //From this currency...
   //To this currency...
   //Obtain the correct method name
   //get a ReflectionMethod for the $name method
   $reflectionMethod=new ReflectionMethod(
      'CurrencyConverter', $name);
   //create an instance of the CurrencyConverter class
   $currencyConverter=new CurrencyConverter();
   //invoke the correct method
   echo $reflectionMethod->invoke($currencyConverter, $amount);

Close Icon
Thanks for your registration, follow us on our social networks to keep up-to-date