Tuesday, February 24, 2009

First Real Crack at PHP5

So I haven't written PHP code in a while, but I figured I'd take a crack at setting up some REST services. The last site (the SAVSERV) I wrote was in PHP4 and being used to coding in JAVA and C, I found it pretty clumsy to use. Of course, at that time I was teaching myself PHP4 so I'm sure many horrible things were done. Things I will have to live with for the rest of my life), but I digress. Tonight I put together some PHP5 code for a simple REST login and wow, has it changed.


...
$sql = "SELECT loginid FROM logins WHERE username = '$username' AND password = '$hashedPass'";
try{
//TODO: put this into a common function
$result = DatabaseTools::runSelectQuery($sql);
if(mysql_num_rows($result) == 1)
{
$row = mysql_fetch_assoc($result);
Response::LoginSuccessResponse($row['loginid']);
}
else
{
Response::ErrorResponse(101,'Could not find your information in our system.');
}
} catch (Exception $e)
{
Response::ErrorResponse(101,$e->getMessage());
}
...


Now I am by no means a good PHP programmer. Self taught PHP4 in a few days and now PHP5 in about 3 hours really, so lay off me if things aren't perfect. But just look at that, actual classes and exception handling? Shocking. I swear, I felt like I was writing Java code. And the walkthrough that the PHP main site provides (http://us.php.net/zend-engine-2.php) is very easy to follow with lots of code samples. For anyone who is used to OOD, PHP5 is very easy to pick up and start going. Now if only I could find a nice project to teach myself Perl or Python...