Joel Hainley : San Francisco Bay Area Software Consultant

  • rss
  • Home
  • About
  • Reading Lists
    • 2008 Reading List
    • 2007 Reading List
    • 2006 Reading List
    • 2005 Reading List
    • 2004 Reading List
    • 2003 Reading List
    • 2002 Reading List
    • 2001 Reading List
    • 2000 Reading List
    • 1999 Reading List
    • 1998 Reading List
  • Software
    • mcalc - a tournament poker utility
    • Joel’s "Super Fancy"(tm) Maximum Heart Rate [MHR] Worksheet

PHPUnit and global variables

April 30, 2008

The first time I did some work with PHPUnit I was writing out tests in preparation for some refactoring. So I put together a bunch of tests for my utility classes, and pure logic classes etc, database access classes, etc. So everything was trucking along wonderfully, things are falling into place and then I run a test that tests some database stuff.

The application was setup with a configuration file that defined all of the database connection information, this file is included by the database library providing a single set of database routines to be shared by the application. Pretty standard stuff here. Perhaps in a nod to the rails/java crowds it could have been put in a non-code file and read in by the application but as far as I’m concerned that’s just personal preference. At some point you’ve got to have some configuration information somewhere and this works as well as anything.

So now that you know how things were setup, we’re back to the PHPUnit test…it bombed, failed, crashed, whatever. After digging into it a bit I discovered that PHPUnit has a problem with munging the global namespace when it runs. Things that are global variables can’t be pulled down into local scope because they don’t exist there anymore. I did some reading and found numerous complaints about this in the PHPUnit bug system, all of them closed with “this issue resolved” when the issue most assuredly had not been solved. I even found a conversation where one of the developers stated that it was not a bug, that the user simply needed to modify their code to work with PHPUnit. Huh? Modify the architecture of your code to work with a testing framework?!?

I spent some time thinking about this and I think it could be said that he’s right, but for the wrong reasons. First off, if this is a technical limitation of PHP then just say so and let it go. If it’s an implementation problem with PHPUnit then say so and try to put together plans to fix it. However, I think in this particular instance the problems that I was experiencing with PHPUnit could indicate a larger problem in my application.

Here’s how..One of the things that I hated when I first started using PHP was having to pull things out of the global namespace into the local namespace to use them. When PHP5 came out and the object model finally had a good amount of maturity I moved to utilizing classes for most of my code and this allowed me to start using the $this operator for accessing a lot of variables that I needed to deal with. In fact, the only place I was really using globals was in my library routines. After thinking about this for a bit I realized that it was much more convenient to wrap all of the configuration data into Singleton then I’d have a single object that I could use to get all data instead of having to pull in everything I needed one variable at a time.

It was at that moment that I realized if I had a singleton that housed all of the configuration data I could instantiate it in all of the routines where I needed to access configuration data and it would avoid the problems that PHPUnit had with the global namespace. When I looked in the todo’s for the configuration file there was an item at the top “Maybe I should turn this into a singleton”…two birds with one stone? Ten minutes of work and I had everything setup and ran my tests and “bingo” everything worked.

So in the end, modifying the way that configuration information is disseminated throughout the system by using a singleton has been “the right thing”. I had some heartache over the responses from the PHPUnit developers about the “solution” to the problem their users were having mostly because the solution involved a lot of extra crap with addressing the global namespace in a particular way. Using a singleton ended up providing the needed functionality, didn’t do anything retarded with variable access, and ended up making the application much cleaner to work with.

Comments
No Comments »
Categories
php, programming
Tags
global variables, php, php5, PHPUnit, testing
Comments rss Comments rss
Trackback Trackback

PHP Soap - A Simple PHP Web Service Example

March 3, 2008

There’s no doubt that today web services are hot, all of the commercial platforms ( J2EE, .Net, etc ) have tools that make the development of web services very easy. There are also a bunch of tools that are out there that help people generate their WSDL’s and can generally ease all of the difficult parts of developing web services. The great thing is that with the release of PHP 5 we now have tools that allow us to quickly develop web services in PHP. The great thing about this is that you can stub out api’s and start to have different solutions speak easily among themselves, share business logic to languages and technologies that are “not native”.

Within this article I’m just going to hit some highlights about creating your own web services using SOAP and PHP. I will also provide a URL to a web service that I have written for people to fiddle around with their first SOAP client. Please note that I’m not providing an exhaustive example of all of the details of wsdl’s/SOAP and the like, this is the SOAP equivalent of a “hello world” program, but with parameter passing included since most “Hello Worlds” don’t have parameters passed into them. We’re going to be building a little service that will support 2 operations, rot13 of a supplied string, and the mirroring(reversing) of a supplied string. Nothing terribly fancy, but certainly more interesting than “hello world” ;-).

You can see the WSDL for this service here . There isn’t much to note with the WSDL it is a simple service and I’m not trying to get too crazy here. So take a look at it and see if it makes sense, if not, try to compare it against the code samples below and see if you can determine how it all relates.

Now that we have the WSDL defined it’s time to build the SOAP server to support this WSDL. Here’s the full listing for the server :

function getRot13($pInput){
$rot = str_rot13($pInput);return($rot);
}

function getMirror($pInput){
$mirror = strrev($pInput);

return($mirror);
}

// turn off the wsdl cache
ini_set(“soap.wsdl_cache_enabled”, “0″);

$server = new SoapServer(“scramble.wsdl”);

$server->addFunction(“getRot13″);
$server->addFunction(“getMirror”);

$server->handle();

Pretty easy to understand, you’ll notice that we are just defining the functions at the top, then once we have created the soap server using the WSDL we simply associate our php functions with the functions defined in the WSDL by passing the function name to the addFunction message of the newly created SOAP server. Note that we have turned off the caching of the WSDL, this is something you’ll wanna do while you’re developing. When you don’t do this and make changes to your WSDL and they never seem to take, it can get frustrating ;-).

Ok with the server out of the way let’s turn our attention to the client. Here’s the listing for a simple client :

// turn off the WSDL cache
ini_set(“soap.wsdl_cache_enabled”, “0″);

$client = new SoapClient(“http://www.joelhainley.com/examples/soap/scramble.wsdl”);

$origtext = “mississippi”;

print(“The original text : $origtext
“
);
$scramble = $client->getRot13($origtext);

print(“The scrambled text : $scramble
“
);

$mirror = $client->getMirror($scramble);
print(“The mirrored text : $mirror
“
);

I’m sure you could write a fancier example, but it gets the point across. First, You turn off the WSDL caching on your client as well as the server. Then you create a new soap client using the wsdl. Then you just start calling methods that you defined in the server. That’s about all there is to it.

Note, I’m going to keep the Soap Server referenced in this article up for you all to use. Please feel free to use it for testing your clients as you’re getting up and running. Eventually you’ll wanna start using your own servers for more interesting things though ;-)

Comments
4 Comments »
Categories
php, programming
Tags
php, php soap example, php web services, php5, soap, soap client, soap server, web services
Comments rss Comments rss
Trackback Trackback

hamtesting.com needs some SEO

February 12, 2008

History  

I finished the basic functionality of hamtesting.com in July 2007. I wrote the whole thing in Ruby/Rails in a week and then spent some time trying to deploy it, dealing with a bunch of issues related to inefficient xml processing etc, making it play nicely with apache and whatnot. While sitting at a Super Happy Dev House I finally had enough with trying to deploy Rails when I already knew all of the issues with PHP, so I sat down and rewrote it in PHP.   I launched the basic testing module of the site about a week later after I made it a little bit prettier( all css based ). I then wrote the review module and released that about 2 months later.

Getting The Word Out

I made a couple of lame attempts at getting some visitors to the site. I made some posts to some of the enthusiast groups and told people that I met about it, hoping to generate some interest. However, I naively thought that it would get itself found by the internet since it was vastly superior to most of the free testing sites, and even superior to most of the pay software/sites. Unfortunately it never seemed to generate much interest, I have some guesses at number of visitors in a given month and it’s VERY VERY OBVIOUS that the word ISN’T getting out.

 Using the Brain

Highly underrated, I used my brain to think about why people weren’t using the site. Then I typed in “ham testing” and “amateur radio testing” and “arrl testing” into google and started clicking back through the pages. I never did find my website. So how are others supposed to find it?

The Plan and The Goal

They say you need a goal and a plan to get there. So I have a goal. Hamtesting.com showing up on the first page of results when some keywords I’ve identified as being the most relevant are searched on. The plan is all laid out, I’d like to get triple the traffic of my biggest month thus far by the end of April, and then I’ll set some more goals.

The Baseline

In an effort to measure progress I’m going to put up some very unscientific numbers for the last two months :

january 2008 : 102 page impressions

december 2007 : 65 page impressions

november 2007 :  63 page impressions

So with those numbers as a basis perhaps we can see how effective my efforts are at generating some users for the site. I really believe the product is top notch and can improve people’s chances at passing their tests and learning what they have problems with quickly. It’s top notch, just gotta get the users!

Comments
1 Comment »
Categories
Amateur Radio, business, hamtesting.com, seo, uISV
Tags
Amateur Radio, amateur radio test, arrl, ham radio, ham test prep, ham testing, isv, php, rails, ruby, seo, uISV
Comments rss Comments rss
Trackback Trackback

Navigation

  • .NET
  • 30-Day-Challenge
  • Amateur Radio
  • bicycle
  • books/reading
  • business
  • c#
  • cackl
  • Exercise
  • flex
  • friends
  • hamtesting.com
  • knuth
  • lectures
  • life
  • observations
  • people watching
  • personal finance
  • philosophy
  • php
  • picprep
  • programming
  • reading
  • santa barbara
  • scheme
  • school
  • seo
  • uISV
  • Uncategorized
  • web services
  • website

Search

Archives

  • December 2008
  • November 2008
  • October 2008
  • September 2008
  • August 2008
  • July 2008
  • May 2008
  • April 2008
  • March 2008
  • February 2008
  • January 2008
  • November 2007
  • October 2007
  • September 2007
  • July 2007
  • June 2007
  • May 2007
  • April 2007
  • March 2007
  • January 2007
  • October 2006
  • September 2006
  • August 2006
  • July 2006
  • June 2006

Site Pages

  • About
  • Reading Lists
    • 1998 Reading List
    • 1999 Reading List
    • 2000 Reading List
    • 2001 Reading List
    • 2002 Reading List
    • 2003 Reading List
    • 2004 Reading List
    • 2005 Reading List
    • 2006 Reading List
    • 2007 Reading List
    • 2008 Reading List
  • Software
    • Joel’s "Super Fancy"(tm) Maximum Heart Rate [MHR] Worksheet
    • mcalc - a tournament poker utility

friends

  • Ashley McNamara Photography
  • Colin McNamara

my websites

  • HamTesting.com
  • Photos
rss Comments rss valid xhtml 1.1 design by jide powered by Wordpress get firefox