Tantalizing Remark on PHP and Web Services

    Harry Fuecks
    Share

    Davey Shafik has posted a tantalizing remark on generating WSDL from PHP code, related to some work he’s doing. There’s nothing really to see yet, other than this example;


    Why is this interesting? The answer takes a little explanation…

    If you’ve read web services demystified you’ll know WSDL (Web Services Description Language) is an XML markup that allows you to describe a web service in a manner that a computer can use. When building a client to a web service, it cuts out a lot of manual effort, as you can see by comparing the client1.php vs. client2.php examples, in Zend’s article on the new PHP SOAP Extension.

    So for building web service clients, when using WSDL, it’s remarkably simple with PHP (and other dynamically typed languages). The same cannot be said though, when it comes to building servers with PHP, where WSDL generation is concerned.

    In WSDL, when describing the arguments a SOAP method accepts and the value it returns, it’s expected that you use XML Schema. XML Schema is “strongly typed” – it has the full range of primitive types (e.g. string, int etc.) as you can see here from which you can build complex types to represent things like objects and hashes (associative arrays). The only thing XML schema doesn’t “do” is indexed arrays, which instead is defined in WSDL.

    At this point I could rant about SOAP / WSDL being a debacle, but I won’t, expect for one comment: if you’re ever in the situation of evaluating whether to use SOAP, begin by researching what SOAP stands for (somewhere between “Simple Object Access Protocol” and “Service Oriented Architecture” you may start to get nervous).

    Anyway – the problem for dynamically typed languages (those where types are explicitly declared in the source code) is how do you automate the process of generating WSDL from code? As a developer of a web service, ideally you want to be able to write your code in PHP then have some program generate the WSDL for you, to save time, eliminate errors etc.

    In languages like Java or C#, where you’re forced to declare types, it’s no problem, for example;


    public class Calculator {
    public int add(int i1, int i2) {
    return i1 + i2;
    }
    public int subtract(int i1, int i2) {
    return i1 - i2;
    }
    }

    The add and subtract methods declare what types of parameter they accept as well as their return values, so it’s simply a matter of using a tool which parses the source code and generates WSDL. But consider the same in PHP;


    class Calculator {
    function add($i1, $i2) {
    return $i1 + $i2;
    }
    function subtract($i1, $i2) {
    return $i1 - $i2;
    }
    }

    Although it’s probably clear to you and me, looking at this code, that the values involved should all be integers, how does a program, analysing this code, work that out?

    To make like even more interesting, PHP allows you to accept and return wildly different types, depending on runtime circumstances. For example I could modify the add method above to allow me to add the elements or two arrays;


    class Calculator {
    function add($i1, $i2) {
    if ( is_array($i1) && is_array($i2) ) {
    $i1count = count($i1);
    if ( $i1count == count($i2) ) {
    $sums = array();
    for ($i=0; $i < $i1count; $i++) { $sums[$i] = $i1[$i] + $i2[$i]; } return $sums; } else { return FALSE; } } else { return $i1 + $i2; } } // ... }

    The value returned by the add method now depends on the values I give it. Trying to generate WSDL automatically in this case is clearly going to be a problem - the parser would need to have a very deep understand of PHP's syntax.

    As an aside, there are echoes of the same problem when it comes to writing "compilers" which turn PHP into some other, faster, form (see George Schlossnagle's Roadsend PCC, micro-review).

    Anyway, watch this space for more on generating WSDL from PHP. Would be great if Davey can pull it off.