Category Archives: Coding

Building a SOAP Server with Zend_Soap_Server and Returning Arrays of ComplexTypes

ZendFramework is awesome but suffers from a peculiar lack of thorough examples and documentation.

To save you all some time scouring the internet fruitlessly, I will document how to use Zend_Soap_Server, and Zend_Soap_AutoDiscover in combination with Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex (try saying that really fast a few times!).

A Complete Example

No one ever put together a full example in the documentation, their blog posts, the official Zend samples or anywhere else I searched yesterday. It’s always snippets here and there, and they don’t fit together well. So let me save you some trouble. I figure your reaching this page means you’ve already passed the first test which is to know the classes you need, which is why the search brought you here :)

I’m binding my webservice to a class, as that would be the logical choice in most cases.

/* these are required for it all to work */

/* my system makes use of a global prefix, feel free to do as needed */
$serviceURL = WWWPREFIX . '/webservices/myservice';

public class MyService {
     * @param integer $UserID
     * @return array
    function GetCoupons($UserID) {
        // do some work here
        $coupons = array(new Coupon(), new Coupon());

// Generate WSDL relevant to code
if (isset($_GET['wsdl'])){
    $autodiscover = new Zend_Soap_AutoDiscover('Zend_Soap_Wsdl_Strategy_ArrayOfTypeComplex');
} else {
    $server = new Zend_Soap_Server($serviceURL . "?wsdl");
    $server->setObject(new MyService());

There are a few points of interest in the code block above (which is a completely functional service btw):

  • Notice the PHPDoc block above the function? If you don’t get that right, it won’t work at all
  • The return type of array because we’re returning an array of objects
  • The Zend_Soap_AutoDiscover bit at the bottom? That’ll auto-generate your WSDL for you

There, you’re done. It should all just work, complete with WSDL auto-generation. If you’re looking to make use of an existing WSDL file, you can use the secondary method of binding to methods/functions as outlined over at the official documentation on Zend_Soap_Server.

Say thank you Uncle Kevin :)

SVNKit + Subclipse Problems

If you’re experiencing difficulty getting Subclipse working with SVNKit, install the very latest. One of the dependencies pulled in will list an SVNKit beta. Install that one and it’ll suddenly show up as a provider.

This is true as of Eclipse Ganymede and Subclipse 1.6.2 (installed with CollabNet Desktop). Just thought to save someone else several hours of their life :)

On CodeIgniter

As taken from the CodeIgniter website:

CodeIgniter is a powerful PHP framework with a very small footprint, built for PHP coders who need a simple and elegant toolkit to create full-featured web applications. If you’re a developer who lives in the real world of shared hosting accounts and clients with deadlines, and if you’re tired of ponderously large and thoroughly undocumented frameworks

I must say that after looking through the documentation, watching the introductory screencasts and then experimenting with it myself, it really does live up to it’s claim of real-world thoughtfulness. CodeIgniter flexible and clear when it comes to the MVC pattern, and all through the tutorials I never once felt that I didn’t quite understand what was going on. Even reading the documentation, it’s all amazingly well explained.

I’ve so far not felt mystified by something in the framework, and haven’t yet had to ask any questions on the forums of IRC channels — something I’ve had to do numerous times with CakePHP, a similar open-source project. This of course isn’t to say that Cake is bad, but good documentation is king when it comes to programming.

Overall, the first impression you get when you run CodeIgniter is that of confidence. This is of course the benefit of having a commercial entity backing a project — little things like the documentation that typically don’t get done with a non-commercial project get taken care of.

Best of all? CodeIgniter comes with a license that qualifies as ‘Open Source’.

Lookout CakePHP, you may have the major portion of mindshare right now, but CodeIgniter is a serious contender.

FizzBuzz in PHP

I was reading Why Can’t Programmers… Program today and while I find questions like these annoying at interviews because PHP isn’t really used for that sort of thing, I set myself and my colleagues the task of writing out answers.

The question:

Print out the numbers 1 to 100. Where the number is a multiple of 3, print ‘Fizz’, otherwise if it is a multiple of 5 print ‘Buzz’. If the number is a multiple of 3 and 5, print ‘FizzBuzz’.

Here are our answers, and mind you, they’re all in PHP.

Lim is our budding project coordinator turned programmer. He’s just started getting into PHP, and here’s what Lim came up with:

for ($count=1; $count<=100; $count++)
    if ($count%3 == 0 && $count%5 ==0){
        print "FizzBuzz<br />";
    elseif ($count%3 == 0){
        print "Fizz<br />";
    elseif ($count%5 == 0){
        print "Buzz<br />";
    else {
        print $count."<br />";

An almost perfect textbook style answer.

Here’s what Foong came up with:


    if(!preg_match("[\.]" , $result)&&!preg_match("[\.]" , $result2))
        echo "FizzBuzz<br />";
    elseif(!preg_match("[\.]" , $result))
        echo "Fizz<br />";
    elseif(!preg_match("[\.]" , $result2))
        echo "Buzz<br />";
        echo $i."<br />";

Certainly an example of … unique thinking. Foong’s had more than a year of experience programming in PHP, for the record.

Here’s what I came up with:

foreach(range(1,100) as $i)
    echo $i % 3 == 0 ? ( $i % 5 == 0 ? "FizzBuzz\r\n" : "Fizz\r\n" ) : ( $i % 5 == 0 ? "Buzz\r\n" : $i."\r\n" );

Update: I’ve made a change to the code, Ken Brush a.k.a. shiruken noticed I’d missed something out :(

I love the conditional operator @_@

Also, no, I would never use code like this in production, or even casual coding. I just wanted to beat my colleagues on line-count :P

And now, the ‘proper’ textbook solution for all the Googlers:

for ($i = 1; $i <= 100; $i++)
    if($i % 3 == 0 && $i % 5 ==0){
        print "FizzBuzz<br />";
    else if($i % 3 == 0){
        print "Fizz<br />";
    else if($i % 5 == 0){
        print "Buzz<br />";
    else {
        print $i."<br />";