Problem: The methodTable property can be a real pain to maintain updated throughout the creation of a service class. It's a lot of coding/writing when dealing with big classes. Changes and additions to the service class require you to update the methodTable which can be very time consuming.
Solution: Automate the creation of the methodTable.
Discussion: When you look at the structure of a methodTable, you'll find the different method names, descriptions, access properties, parameters, etc.
-
"description" => "Returns list of park types",
-
"access" => "remote",
-
"description" => "Shows list of parks given a park type",
-
"access" => "remote",
-
"description" => "Return details on a park give the parkname",
-
"access" => "remote",
-
)
-
);
Most of these properties are allready in your code when creating a method. You write down the name of your method and add parameters as your method needs them. So adding them to your methodtable is really double work. In an ideal situation you've also added comments to your code. These can hold a description for the method and, writing a service class, also have its access defined.
PHP 5 now comes with a reflection API which lets you reverse-engineer a class. Using it, we can easily look up the properties we need to add to the methodtable. The method's name and parameters for instance, but also the comment that comes with the method. The description and access property need to be filtered out of the comment.
Putting this all together, we have the ability to create the methodTable in an automated way. After writing a class that does so, the only thing we need to do is call that class in the constructor of our service class.
Following is the source code of the MethodTable class I wrote.
In order to use it, follow these steps.
- include the class in your webservice class
-
require_once("MethodTable.php");
- In your constructor: call the create method and assign its return value to the methodTable property of your class.
-
$this->methodTable = MethodTable::create($this);
Here's the source code:
-
/**
-
* MethodTable class.
-
*
-
* This class uses the Reflection API introduced in PHP 5 to create the methodTable
-
* property for a service class. In order to create it, the MethodTable::create()
-
* method must be called in the constructor of the service class.
-
*
-
* Example:
-
*
-
* require_once("MethodTable.php");
-
*
-
* class MyService
-
* {
-
* function MyService(){
-
* $this->methodTable = MethodTable::create($this);
-
* }
-
* }
-
*
-
* The method's name and parameters will be read from the method signature.
-
* To add the description and access param to the methodTable, you must write them
-
* down in the comment of your method using PHPDOC comment syntax.
-
*
-
* The description is just plain text.
-
* The access param uses the @access tag.
-
*
-
* Example:
-
*/
-
/**
-
* Returns the list of users.
-
* @access remote
-
*/
-
/**
-
*
-
* @author Christophe Herreman (http://www.herrodius.com)
-
* @version 1.0
-
* @since 12/11/2004
-
*/
-
-
class MethodTable
-
{
-
/**
-
* Creates the methodTable of a service class.
-
*
-
* @param $serviceClass The reference to the service class.
-
* @return Array
-
*/
-
$class = new ReflectionClass($className);
-
$methods = $class->getMethods();
-
-
foreach($methods as $method){
-
$reflectionMethod = new ReflectionMethod($className, $method->name);
-
$methodComment = $method->getDocComment();
-
-
$methodTableEntry['access'] = MethodTable::getAccess($methodComment);
-
$methodTableEntry['description'] = $reflectionMethod->getDocComment();
-
$methodTableEntry['arguments'] = MethodTable::getParameters($reflectionMethod->getParameters());
-
-
$methodTable[$method->name] = $methodTableEntry;
-
}
-
-
return $methodTable;
-
}
-
-
-
-
/**
-
* Returns the access property of a method.
-
*
-
* @param $methodComment The comment of the method as returned from the reflection API.
-
* @return String
-
*/
-
return MethodTable::getTagComment($methodComment, "@access");
-
}
-
-
/**
-
* Returns the parameters from a ReflectionParameter list.
-
*
-
* @param $reflectionParameter An instance of ReflectionParameter.
-
* @return Array
-
*/
-
-
foreach($reflectionParameter as $parameter){
-
$parameters[] = $parameter->getName();
-
}
-
-
return $parameters;
-
}
-
-
-
-
/**
-
* Returns the comment next to a tag.
-
*
-
* @param $comment The comment of the method.
-
* @param $tag The tag to return its comment from.
-
* @return String
-
*/
-
-
if ($tagPosition === false){
-
return;
-
}
-
-
-
return $tagComment;
-
}
-
}
Update: I added this file to the AMFPHP CVS today (10/01/2005).
The project can be viewed at http://www.sourceforge.net/projects/amfphp/
Add to Bloglines - Digg This! - del.icio.us - Stumble It! - Twit This! - Technorati links - Share on Facebook - Feedburner
Christophe Herreman is a software developer living in Belgium. He's working on high-end Flex and AIR solutions at
January 9th, 2005 at 7:33 pm
nice
maar als ik de code wil kopiëren, worden die regelnummers mee gekopieerd
wel vervelend
January 10th, 2005 at 9:06 pm
Hallo,
De file werd toegevoegd aan de AMFPHP CVS. Zie update in post.
Christophe
February 7th, 2008 at 12:56 am
Perfect guide .I hope you write more about amfphp in future