PHP 4 Functions and PEAR XML_RPC
-
Lately, I’ve been working with the PEAR package XML_RPC on multiple projects. Overall, the package is well done but there are some limitations of PHP 4 that make using it frustrating in some ways.
The
XML_RPC_Server
class allows you to create a dispatch map of XML-RPC method calls to PHP functions. The means by which it calls these functions is with PHP’scall_user_func()
. However, only functions in the global scope can be called. If your XML-RPC API functions are contained in their own file, this isn’t quite as bad as it sounds.$yourObject = new YourObject(); function rpc_ping() { global $yourObject; $yourObject->doSomething(); }
While it’s certainly not ideal, it’s reasonable. As long as the RPC server code is usually isolated in its own PHP file, it stays manageable.
It’s a good idea to name all of your functions that are mapped to RPC methods with the same prefix, such as
rpc_
. This keeps them somewhat grouped, even if they can’t be in a class. Having them all named with the same prefix would lead one to believe that the dispatch map could then be built automatically by using PHP introspection. However, this is problematic due to PHP 4’s case insensitivity.The function
get_defined_functions()
will return the names of all of the functions in the global scope, however the function names will be returned in all lowercase. This is true of get_defined_functions() in both PHP 4 and PHP 5 as of 5.0.3. However, it should be noted that in PHP 5, the functionsget_class()
,get_parent_class()
, andget_class_methods()
will return method names in the proper case that they were declared. These three functions are case insensitive in PHP 4, and return all lowercase.XML-RPC method calls are case sensitive, so using
get_defined_functions()
is not suitable. I have seen at least two XML-RPC implementations for PHP where method calls are case insensitive due to this problem, which is incorrect. I also saw this technique proposed as an addition to the XML_RPC package in the PEAR bugtracker, however thankfully it was not accepted. XML-RPC servers written in PHP need to comply with the specification, regardless of the “features” of the language.I think that the best way to build the dispatch map is still by hand. This is tedious but it has the advantage of forcing you to think about each function being mapped, and it also gives you the opportunity to define a
docstring
andsignature
for each method in the map.