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

<html> <head> <h3>Currency converter</h3> </head> <body> <form method="get" action="currencyconverter.php"> <b>Convert this amount:<br /><br /></b> <input type="text" value="1" name="amount"> <br /><br /> <hr> <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> &nbsp;&nbsp; <select name="currency2" size="1"> <option value="EUR">EUR <option value="USD">USD <option value="GBP">GBP</select> <hr> <input type="submit" value="Exchange"> <input type="reset" value="Reset"> </form> </body> </html>

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:

<?php class CurrencyConverter { function __construct() {} function EUR_USD($eur) { $usd=1.38745*$eur; return $eur.' EUR = '.$usd.' USD '; } function USD_EUR($usd) { $eur=0.7274*$usd; return $usd.' USD = '.$eur.' EUR '; } function GBP_EUR($gbp) { $eur=1.44610*$gbp; return $gbp.' GBP = '.$eur.' EUR '; } function EUR_GBP($eur) { $gbp=0.69151*$eur; return $eur.' EUR = '.$gbp.' GBP '; } function GBP_USD($gbp) { $usd=2.00640*$gbp; return $gbp.' GBP = '.$usd.' USD '; } function USD_GBP($usd) { $gbp=0.49840*$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:

<?php //include the CurrencyConverter class include("CurrencyConverter.inc"); //Get the amount to be converted $amount=$_GET['amount']; //From this currency... $currency1=$_GET['currency1']; //To this currency... $currency2=$_GET['currency2']; //Obtain the correct method name $name=$_GET['currency1'].'_'.$_GET['currency2']; //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); ?>

Comment and Contribute






(Maximum characters: 1200). You have 1200 characters left.