One man’s voice Thoughts, rants and commentary from a husband, father of five and professional web geek

23Oct/090

Newest title: Book reviewer

A few days ago I received an email from Packt Publishing, "a UK based publishing firm specializing in focused IT books" asking if I would be willing to review a new book they are publishing entitled Zend Framework 1.8 Web Application Development. The book, written by Keith Pope, appears to cover the basics of using the Zend Framework to build web applications rapidly.

According the book description:

The Zend Framework has a flexible architecture that lets you build modern web applications and web services easily. The MVC components make the maintenance and testing of your applications easier. However, it is not only an MVC framework for developers. It also provides an easy-to-use high-quality component library that is designed to be used the way you want, picking up specific components without requiring the use of whole framework.

It's easy to get started and produce a powerful and professional looking web site when you've got this book to hand. Taking you through a real-life application, it covers the major Zend Framework components, as well as throwing light on the best practices and design issues faced when building complex MVC applications.

This book takes you through detailed examples as well as covering the foundations you will need to get the most out of the Zend Framework. From humble beginnings you will progress through the book and slowly build upon what you have learned previously. By the end, you should have a good understanding of the Zend Framework, its components, and the issues involved in implementing a Zend Framework based application.

I am actually looking forward to reading what Keith has put together. With some of the more recent improvements to the Zend Framework I have been really considering putting the framework to use in a project. Hopefully having examples to follow will point me in the proper direction.

Regardless, I am looking forward to reading what Keith has to say about developing with the Zend Framework. And if nothing else, at least I get a free chapter of a new PHP book out of the deal. ;)

22Oct/091

ZendCon 2009: Day 3

And so ends the Zend PHP Developers Conference for 2009...

The last day of ZendCon is always a mixed bag when it comes to wrapping things up. On the one hand you, as a programmer, are loving the learning and education and networking and mixing things up with your peers. But on the other hand you, as a regular person, are tired from all the learning and education and networking and mixing things up with your peers (and drinking... and eating... and more drinking). Still, when all is said and done, all good things must come to an end. And so it goes with ZendCon.

Today started out pretty much the same as the other days except we knew we were in for a half day of sessions instead of a whole day. With that we chose the sessions we wanted to attend and headed for our rooms.

Session 0: Design Patterns for PHP developers
This was the second talk of Cal's that I attended this ZendCon and, much like the first, Cal gave a great presentation on Design Patterns for PHP. Covering the basics of design patterns, he made sure to mention the most important patterns we could use, like Factory, Strategy, Observer, Model-View-Controller, Facade and Singleton. He made the samples very easy to understand and was quick to answer the questions asked of him.

Design Patterns have a special place in my heart as a programmer (I have given talks to local meetup groups about design patterns) and I love how knowing design patterns levels out the disparity between programming languages. Knowing them makes one language for all programmers and allows anyone from any language background to be able to communicate programming concepts and paradigms in a common tongue, if you will. That said, Cal delivered big-time in his talk on patterns. He also inspired me to pursue my ambition of putting together a PHP Design Patterns web site for developers that have not seen, or do not understand, design patterns applied to PHP.

Slide for Cal's presentation can be downloaded here. (.pdf)

Session 1: Authorization with OAuth
Rob Richards gave a talk about OAuth and the difference between authentication and authorization. He moved pretty quickly into an actual implementation of using OAuth for authentication of a user from an application and, in my opinion, moved a little too fast into too concrete of an implementation. Still, the presentation was very informative and useful for anyone that is thinking of utilizing OAuth as an authentication mechanism.

Day 3 && ZendCon wrap-up
I was a little saddened to see the end of the conference come up so fast. Still, I am glad its over because I can use the rest.

I'm thinking next year I want to see if I can get in on giving a talk. I'd love to present and would love to not have to pay to get into the conference :) . I also think that it would be freaking awesome to have a small group of the guys I have met over the last two hears at ZendCon over to my home for a BBQ or something, though I should probably bring that up with my wife before I even think of doing anything like that (I will honey, I promise :P ).

Overall, this year's conference was better than last years in a couple of ways. First, the subject of many of the talks was NOT scaling. That, in and of itself, made this conference better than last year.

Second, the location was also a bit better than last year, being closer to downtown and all that being in downtown has to offer.

On the other hand, this year's conference was deficient in several area. First off, the lack of power in the lobby and in the conference rooms was painful. Having a laptop battery with only a 38% capacity made it difficult to attend a session that didn't have sufficient power.

Secondly, the vendor fair this year kinda sucked. It was nice to have them there, as always, but they didn't seem to want to interact with people this year. The shirts were awesome and the shwag was also very nice -who doesn't like free, right? Still, I think having more relevant vendors with more accessibility to relevant and pertinent information would have been nice.

Regardless, I got nothing but love for the organizers of this conference. Specifically, kudos need to go out to Eli White (@eliw) for his incredible support of the conference and attendees, and Keith Casey (@CaseySoftware) for the indescribable work he does on the conference uncon sessions. I cannot wait for ZendCon2010.

21Oct/090

ZendCon 2009: Day 2

Yesterday was an amazing day at ZendCon. Lots of talks, lots of information, lots of networking, lots of geekdom. The day was long but very informative and at the end of it I couldn't wait to get back for today.

Today started off a bit different than yesterday because today I was scheduled to take the Zend Framework Certification exam and my coworker was scheduled to take the Zend Certification Exam. So we got to the conference and camped out for the morning to get some last minute studying in.

As we studied we were joined by Aaron Wormus. I'm not sure why, but when presenters and other people who are widely known in PHP are close by I always feel like I am in the presence of a celebrity. Last year I was like a little kid at a football game, meeting everyone, shaking hands, being excited, enjoying the conference. This year? Well, kinda the same, except I haven't seen to have as much time this year to find people to meet.

Still, it was nice to be able to spend a couple of hours trying to familiarize myself with the Zend Framework. The exam preparation study guide is 214 pages long, and as of 12:15, when it was lunch time, I had gotten through about 110 page of it. So much for being totally prepared for the exam.

After lunch was over my coworker and I headed to the exam room where we took our test. And wouldn't you know it... I passed. So now I am not only a Zend Certified PHP Engineer, I am also Zend Framework certified as well. Sweet!

After the certifications were over we were able to get back into the swing of the sessions...

MySQL Server Performance Tuning 101
Cal Evans, filling in for Legaya Turmelle, did an excellent job of describing how to optimize your MySQL server and sent queries to enhance the performance of your applications. Filled with an incredible amount of technical data, this presentation was still a vibrant session that involved the attendees throughout the entire session. And I managed to sneak away two things that will be a help to me as soon as I get back to work:

/* Show all of your global variables */
mysql > SHOW GLOBAL VARIABLES;
 
/* Show your statuses */
mysql > SHOW GLOBAL STATUS;

Both of these can filter your queries by adding a "LIKE '%{filter}%'" to the query. Very handy pieces of information.

Architecting Your Models
Matthew Weier O'Phinney gave an awesome talk about architecting models (the M in MVC) and using your models to handle business logic. The logic that was employed was very similar to the Introduction to Zend Framework talk he gave on Monday (kudos for consistency) and covered data access, table/row gateways and service layers.

This is one of those presentations that I would recommend you get the slides for because just the programming practices employed in his presentation make it an absolutely stellar session and one that can only help you as a programmer. [Note: as soon as the links to the slides are posted I will put that link here. Sorry.]

Building Desktop RIAs with JavaScript and PHP
Ed Finkler gave a sweet presentation on taking PHP and Javascript from the web to the desktop in his talk on building Rich Internet Applications. His talk was dynamic, funny, had a couple of well handled snafus and totally captivated the audience.

Using Adobe AIR, Appcelerator's Titanium and PHP, he built a couple of cool little desktop apps that are driven by server hosted PHP applications. Though the apps were just for example, what you can do with AIR or Titanium, jQuery and PHP is freaking amazing and I can't wait to try some of this stuff when I get back to work.

Links to his presentation and associated code samples:

Day 2 wrap-up
Sitting here, at the end of the last session, I am overwhelmed with the amount of information I learned in the last couple of days. I am so looking forward to how ZendCon will be wrapping up tomorrow and cannot wait to meet some of the people of I haven't yet had a chance to meet.

20Oct/09Off

ZendCon 2009: Day 1

The second day of ZendCon is always a busy day. That is the day the sessions start; when you try as hard as you can to be in three places at once; where you spend half of each session reading Twitter for all #zendcon hash tags or hang out on the #zendcon IRC channel; when all you can think of outside of PHP is free swag and free beer.

Today was like that for me. Well, minus the mad rush to sit in on the morning keynote. I spent the early morning reading up on the Zend Framework so I can stand a chance at passing the ZF Certification Exam tomorrow. There is a lot to learn in that stupid manual (214 pages long - gripes!) so there was naturally lots of reading to do. After that we started the day.

Session 0: PHP 5.3 = Awesome
Ilia Alshanetsky gave this talk on PHP 5.3 and some of the new and exciting features in it. Outside the fact that Ilia never stops talking... I mean never... this talk was filled with some pretty cool stuff that a lot of programmers have been waiting for in PHP.

He opened with talking about namespaces and how they can be used. There are so many ways to utilize the namespace feature that to repeat that here would take a lot of screen real estate. I would recommend getting your hands on the slides of the presentation and reading through the first few slides. There is lots of useful information on namespaces in the slides.

The next thing he talked about was the new usage for the ternary operator. While there are, I'm sure, different ways to implement this feature, the manner in which it is implemented right now is pretty neat. Essentially you can now say "If the first condition evaluates to true, use it, otherwise use the second" without saying it exactly that way:

<?php
// We used to do this with ternary
$var = $a ? $a : $b;
 
// Now we can do this
$var = $a ?: $b;
?>

That is pretty slick in my opinion.

There was also mention of the use, internally, of the MySQLInd client library for MySQL Database Server interaction and how much overhead is being saved on select queries when not manipulating SELECT query result set data. Essentially you can cut your memory consumption in half because of this change. Sweet.

After this the talk moved into some new stuff you can do with INI files and data you can set in them. This change will make configuration pretty freaking sweet in my opinion, just because of what you can set and how you can set it. There is also some pretty nifty stuff that is happening with the DataTime built in objects in PHP. And lastly, there is a new PHP constant being introduced. That constant is E_DEPRECATED and it will be used to tell you, when you have error_reporting set to E_ALL, if what you are using in PHP 5.3 is slated for deprecation in PHP 6.

Overall this was an awesome talk. Ilia is a great speaker, knows his stuff very well and packs in about three hours worth of presentation into about an hour.

Session 1: Xdebug — PHP developer's swiss-army knife
Presented by Derick Rethans, the author of xDebug, this presentation covered an enormous set of the features included in xDebug. Derick also talked about kcachegrind and utilizing xDebug with PHPUnit to cover many aspects of unit testing and debugging

Slides for Derick's talk are available for download at his website.

Session 2: Mastering OpenXML Documents with PHP
Aaron Wormus gave an excellent talk about the PHPExcel library and how it utilizes the OpenXML standard to write complex and feature rich spreadsheets for Microsoft Excel, Open Office and other text based formats. This was very well presented session and covered several different, clearly coded examples.

Session 3: Premature Optimization Mistakes
Another awesome presentation by Ilia Alshanetsky, this session talked about some of the wrong directions programmers can go into when trying to optimize their applications. Many myths were debunked in this session, like the idea that smaller code is better code, removing comments in PHP makes your applications faster and changing your code makes it better. I even installed APC on the PHP Developer's Network forums during this presentation to buy some performance gain without having to do any really heaving lifting.

Again, Ilia put about three hours worth of information into an hour long session and knocked it out of the park.

Session 4: Taming the Deployment Beast
Chris Cornutt, the man behind PHPDeveloper.org and joind.in, gave a hugely informative presentation about deployment and managing your applications from the start of development to the implementation of production over both teams of developers and for individual programmers.

Covering an array of various tools, practices and principles, Chris's presentation was filled with application lifecycle management concepts, deployment practices and low level programming practices.

Slides can be downloaded here

Day 1 wrap-up
Much like last year, the first day of ZendCon was packed with information, networking, free swag (thank God for vendor fairs) and great food. Seriously%2

19Oct/090

ZendCon 2009: Day 0

Woo to the frickin' hoo! ZendCon 2009 is back in town!

I am fortunate enough to work for a company that believes that you should continue your education within your professional field. They back up that commitment by putting their money where their mouth is and actually spending money to send their employees to various training seminars and conferences throughout the year. And this year, like last year, ZendCon is that conference for me.

Like last year, this year opened up with the conference tutorials day. This year I chose to focus on Zend Framework and learning what I could of it. It is a fast growing framework and is quickly becoming a popular tool in the arsenal of many a PHP developer. I am seeing more and more recruiter emails that are asking for ZF experience. And it is a beast of a code base, well written and extremely well organized, developed by a team of people that are some of the biggest and brightest names in PHP development around the world.

Session 0
My day started out with an Introduction to Zend Framework by Matthew Weier O'Phinney. Matthew gave a stellar presentation on using the Zend Framework to develop a pastebin application. Covering some of the ins and outs of the MVC architecture employed by the Zend Framework, he also taught some programming practices as he started out first by developing his models, then his forms, then his controllers then his views. He taught techniques like setting view variables as their lowest PHP variable type so they are not to tightly coupled to a particular implementation should controllers or models change (like passing a result array to the view as opposed to a DB result object).

He also went into a decent amount of detail on Zend_Tool, Zend_Config, plugins and routing. In the end I felt like I could actually do something cool with the Zend Framework. Maybe not as cool as what he did, but cool nonetheless.

Now if I could just figure out those sweet vim plugins he was using...

Session 1
Seeing as the Zend Framework certification exam is being offered to conference attendees for free I figured since I am here that I would try to add to my ZCE certification. To do that I figured I'd sit in on the Zend Framework Certification Refresher course being presented by Rob Allen.

I am pretty sure I was in over my head from the start of this class. One of the first things that Rob said was that this class was not a tutorial on Zend Framework but a refresher for those that are using it regularly. For almost every component that was covered he asked the question "Who has used this?" and sadly I could not raise my hand for hardly any of them. Still, on the practice questions I did alright because much of the Zend Framework is common enough that you can expect certain behavior from it. That seemed to get me some pretty good mileage.

Of course there were lots of things that I had never seen, either. Parts of Zend_Log (and the writers, the formatters and the filters), parts of Zend_Db (the table data gateway and the row data gateway), Zend_Auth (the returns from Zend_Auth::authenticate()) and a host of other items from the framework are fodder for me reading up on.

Still, I think I can pass the exam if I don't overthink the questions being asked. Of course, that means lots and lots of reading tonight, tomorrow, tomorrow evening and Wednesday morning.

Other happenings (and mishappenings)
When my coworker and I got to the conference and registered that we were there we noticed something missing from our registration packet. All early registrants who attended a previous ZendCon were supposed to receive a netbook for the conference. Ours was not there. Throughout the day we checked back in with the front desk, per their instruction, and found out at the end of the day that we would be getting ours tomorrow. That was a small bummer, though it had a happy ending so I am pretty stoked about that.

Something else I noticed this year was the lack of power in the rooms. Last year there was no shortage of power plugs to plug my laptop into. This year, in the first session, only the people that sat next to the walls close to an outlet could plug in. The second session was a little better in that my room had two power strips. Still, both of those were on one side of the room so if you sat on the other side of the room for the three hour session there was a decent chance that you were going to be running out of power before the end of the session.

Wireless internet this year was way better than last year for me (albeit a lot slower). Connecting to it was a snap and once I was able to power up my laptop I was able to stay connected to the internet pretty much the entire rest of the day.

Lunch was really good this year. For a boxed lunch the food was really, really good. As were the dining accommodations. My coworker and I were able to meet some new folks (a fellow from Seattle, John from Vermont and another fellow from Slovenia) as we talked about the ZCE, differences between PHP and Java and this year's conference versus last year's.

Ending the day this year was a little sad for me. Not because the day was over and I had to wait an entire half a day to mingle with my peers again. No, it was because of the $18 I spent on parking at the Convention Center. What. The. Hell? There was no validation either. And to top it off, the stupid ticket reader didn't work so we had to drive around the entire garage until we found a guy that could help us.

Still, given the day as it went, I am very happy with ZendCon this year. I cannot wait until it gets into full swing tomorrow. And I really can't wait for some of the uncons that were just posted late tonight.

16Oct/090

Handling multiple MySQL results in PHP

Back in the day (June of 2007 to be a little more exact) I posted a little snippet about using PHP to hand multiple MySQL query results with the MySQLi extension. At the time it was something I was using just to see what was coming back from the database when I called a stored procedure that happened to have multiple results in it. Skip to how this really works...

Since then I have improved my little handler, added some library code to actually handle queries and results (and timers and requests and... dude, so much). The new code, which is much fatter (not necessarily better, just fatter in that it has a lot more features in it), now encapsulates much of what the original, simple script was doing. Unfortunately, the script is no longer as simple as I wanted it to be. However, it does now come in several files which might be of use to you in some of your own code.

Because of the change that I made to the way this code works, I have decided to package it into a little zip file for your downloading pleasure. The zip file includes:

  • index.php (the actual procedure/result tester)
  • A lib directory, which contains:
    • memorystats.php (A memory use and reporting object)
    • mysql.php (The database connection and query object)
    • mysqlresult.php (The database result object)
    • request.php (The HTTP request object)
    • timer.php (The process timer object)

It should be ready to use right out of the box, with the exception of making sure to add your own username, password and database name to the $db->connect() call (at about line 85 in index.php). I'm sure you could expand it to be more of what you want in a tester (like adding a database chooser to it - right now I have a database server chooser, but not a database chooser within a server) but overall it is a very nice little script that does pretty much what it was built to do: run a query/procedure and return all of the results in it.

Please note: This is seriously not something that you should think to use in a production environment. The farthest this has ever made in any of my architectures is the dev machine. Not that the code is not good. It is just that putting something that has free form access to your database onto a production machine is, in my opinion, a remarkable stupid thing to do and I would hate to have my name associated with code that was used to exploit your business because you chose to put a huge gaping hole into your network through a simple little test script. Just sayin'.

Under the hood
Before diving into that mash of code I included in the zip file, it might be a good idea to see just the relevant code so you can, if you want to, just tap into the multiple result set handling of MySQL results in PHP. Without further ado...

<?php
// Set a query
$sql = "SHOW TABLES FROM `mysql`; SELECT VERSION() AS `version`; SELECT NOW() AS `date`;";
 
// Change the params to you own here...
$mysql = new mysqli('HOSTNAME', 'USERNAME', 'PASSWORD', 'DATABASE');
 
if (mysqli_connect_errno()) {
	die(printf('MySQL Server connection failed: %s', mysqli_connect_error()));
}
 
// Initialize our result set array
$results = array();
 
/** 
 * Check our query results
 * 
 * This is where the magic happens, and it must be this way anytime you use a
 * stored procedure. The reason for that is the MySQL server always sends a 
 * status return with any query, even a select query. That means that even a
 * single result set select query will return two results. Those results will
 * not necessarily make it to your application (I think the client handles 
 * that) but in my experience I have always had to use multi_query to get this
 * to work.
 */
if ($mysql->multi_query($sql)) {
	do {
		// Create the records array
		$records = array();
 
		// Lets work with the first result set
		if ($result = $mysql->use_result()) {
			// Loop the first result set, reading the records into an array
			while ($row = $result->fetch_array(MYSQLI_ASSOC)) {
				$records[] = $row;
			}
 
			// Close the record set
			$result->close();
		}
 
		// Add this record set into the results array
		$results[] = $records;
	} while ($mysql->next_result());
}
 
// Close the connection
$mysql->close();
 
// Set our output type
header('Content-Type: text/plain');
 
// What do we have
print_r($results);

And there you have it. Nice, simple, clean and easy to use all by itself.

13Oct/092

Scope? Nope. Dope!

Today I made a classic blunder in PHP programming. It is one that many make and certainly one I have made before. But the frustrating thing about this issue is it probably would have been caught by a simple unit test or, in lieu of that, something as simple as trying to capture output of my script from the go.

I was working on the command line (something I have been spending a lot more time doing) building a background processing application that will be triggered by the web. The process is expected to take between 45 minutes and an hour each time it is ran and will be ran one time a month at the discretion of an administrator (hence the reason it is not set as a cron job).

One of the very first lines of the CLI script looks like this:

<?php
// Define our path for all includes and file writes
define('APPPATH', realpath('.'));
?>

It would seem simple enough, right? Basically that little line of code defines a constant called APPPATH that would essentially describe the path to the location of the file that is setting the constant. At least that is what I thought. And every test I ran led me to believe that I had indeed made the right choice to set the path the way I did.

I ran the script, and even other scripts that derived code from the script, from the command line on my local Ubuntu machine, from the command line on our Fedora dev machine and from the command line on our Ubuntu production server. I have similar snippets that are working on all of our machines, and these snippets work interactively, through cron and through the web.

But today something happened that I did not intend. As I attempted to run my last process test of this long running script through the web I found myself in a place where my script would not run. It was being triggered properly. It was just not actually doing anything.

After an hour of trying to figure out what I was doing wrong I solicited the wisdom and advice of two of my colleagues who are seasoned Unix professionals and after a couple of minutes I was able to begin to see what was wrong. Can you guess what it was?

That's right... PHP, on the web, was applying its scope to the CLI script that it was calling. The reason it had worked in all cases prior to triggering it by the web was that I was logged in as me and calling the script as a CLI script interactively, through the prompt. That means that the setting of the APPPATH constant was happening as expected and to what I expected.

However, from the web, when PHP runs as an apache module, it calls the CLI script from the scope of the web server. That means that the use of realpath() on the location '.' (current working directory) was assuming the working directory was the web server root, not the location of where the script actually resided.

That's right folks, I lost track of the scope in which I was working. Like a dope.

The simple solution to this problem was to change where the script looks to set its own directory. Can you think of what I could have used, instead of '.' as the location to pass to realpath() so that it knows, without a doubt, what its own path is?

<?php
// Get the real path to the current directory location OF THIS FILE
define('APPPATH', realpath(dirname(__FILE__)));/
?>

I hate it when something this simple causes such deep pain and suffering, needlessly.

4Apr/098

Getting books details by ISBN in PHP

Sometimes I love being a geek. Today, as I set out to inventory my collection of almost 500 books or so, I wanted to find a fast way to get the information on the book I wanted based on the books ISBN. This was because I was already getting tired of typing five books into the inventory (yes, I am that lazy) and I really did not want to keep typing out the authors name, the title and the subtitle for each book.

So I went to ISBNdb.com and entered an ISBN thinking I would be able to just copy and paste the book information from the output. That turned out to be overwhelmingly difficult since their output of the search is really convoluted. I knew what I wanted to get and they didn't offer that.

But they did offer an API and, after reading their docs, I realized that I could write a script that would take an ISBN and return to me the information I was looking for in a way that I could just copy and paste it. Remarkably, it was faster and easier than I thought it would be. I actually developed this little snippet, freely available for you here, in about 5 minutes. Hope it helps you in some way, if you are looking for such a thing.

NOTE: Before using this code make sure to sign up for your own API key. In order to do that you will need to register for an account with ISBNdb.com and then create a key. But as soon as you do you will have immediate access to their API. The API is simple, responding to the request with a simple XML output. Anyhow, without further ado, here is the little script I put together to fetch me the data I was looking for the way I wanted it.

<?php
/**
 * Set this value to your own API key
 */
$apikey = '12345678';
 
/**
 * Initialize this var for use when forms are not posted
 */
$isbn = null;
 
/**
 * Initialize the result set var
 */
$rs = null;
 
/**
 * See if the form is posted
 */
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  /**
   * Get the ISBN from the form
   * 
   * No, there is no validation on this. This 
   * was for me and I knew I would not be at
   * all trying to trick my own script. USE 
   * THIS AS IS AT YOUR OWN PERIL.
   */
  $isbn = $_POST['isbn'];
 
  /**
   * Get the result as a SimpleXML object
   */
  $rs = simplexml_load_file("http://isbndb.com/api/books.xml?access_key=$apikey&index1=isbn&value1=$isbn");
}
?>
<html>
<head><title>ISBN Check</title></head>
<body>
<form method="post" action="<?php echo basename(__FILE__) ?>">
  <p>
    Enter an ISBN:<br />
    <input type="text" size="40" name="isbn" id="isbn" value="<?php echo $isbn ?>" />
  </p>
  <p>
    <input type="submit" name="submit" value="Search ISBN" />
  </p>
</form>
<?php
/**
 * Only output more stuff if there is a result
 */
if ($rs) : ?>
<hr />
Results for ISBN: <?php echo $isbn ?>:
<?php 
/**
 * Results for a book are in $rs->BookList->BookData
 * 
 * You can see this using var_dump($rs);
 */ 
$book = $rs->BookList->BookData;
?>
<p>
  <strong><?php echo $book->Title; ?></strong><br />
  <?php echo !empty($book->TitleLong) ? $book->TitleLong . '<br />' : null; ?>
  <?php echo $book->AuthorsText; ?><br />
  <?php echo $book->PublisherText; ?><br />
  <?php echo $isbn; ?>
</p>
<?php endif; ?>
</body>
</html>

Typical output might look like (from one of the books I was checking on):


Results for ISBN: 0316116955:

The day the universe changed
James Burke
Boston : Little, Brown, c1985.
0316116955


I hope you enjoy this, if you happen to be looking for an ISBN search tool that you can run on your own. ;)

3Apr/090

A small cache routine in PHP

It has been a long time since I have posted anything PHP related so I decided that to end the week I would post something related to caching data to a file. File based cache's are probably not the sexiest thing around and more than likely not the best way to do things. But in my case I needed something light and small to house a small data set from a database server that was taking upwards of 35 seconds to return 20 rows of data.

I also only needed this cache in one place so in my original code I have it running a small procedural routine within a method. But I figured I would extend that process to be a little more flexible so I objectified it. Here is what I came up with:

NOTES:

  • The backend data is coming from a database in my sample. It could legitimately come from any place that takes a long time to retrieve though.
  • I am using a MySQL database backend in this example, driven by PDO
  • The database handle is assumed to have been created and is retrieved from a registry object in the example
  • The directory where the cache file will live will need to be writable by the web server
  • There is very little error handling in this code. I will leave that up to you.
  • Some of what is being done is tightly coupled internally and requires overwrites to change (path, filename, etc)
  • I have provided precious few examples.
<?php
/**
 * Small abstract class that handles the non-specifics of the cache routine
 */
abstract class SmallCache {
  /**
   * Directory path to the cache file 
   * 
   * @access protected
   * @var string
   */
  protected $_cachePath;
 
  /**
   * Name of the cache file
   * 
   * @access protected
   * @var string
   */
  protected $_cacheFile;
 
  /**
   * Flag that checks to see if the file is writable
   * 
   * @access protected
   * @var boolean
   */
  protected $_cacheFileWritable = true;
 
  /**
   * Default expiration time of the cache
   * 
   * @access protected
   * @var int
   */
  protected $_cacheExpiry = 3600;
 
  /**
   * Right now
   * 
   * @access protected
   * @var int
   */
  protected $_currentTime;
 
  /**
   * Object constructor
   * 
   * @access public
   * @param string $cacheFile Name of the file to write to
   * @param string $cachePath Path to the directory where the file will live
   * @param int $cacheExpiry Time in seconds the cache should stay alive
   */
  public function __construct($cacheFile = null, $cachePath = null, $cacheExpiry = null) {
    $this->setPath($cachePath);
    $this->setFile($cacheFile);
    $this->setExpiry($cacheExpiry);
    $this->_currentTime = time();
  }
 
  /**
   * Abstracted method that each child should implement
   */
  abstract public function fetch();
 
  /**
   * Simple sets a path if one is given
   * 
   * NOTE: The path should end with a slash 
   * 
   * @access public
   * @param string $path The path to the directory where the cache files live
   */
  public function setPath($path) {
    $this->_cachePath = $path ? 
      rtrim($path, '\/') . DIRECTORY_SEPARATOR : 
      realpath('.') . DIRECTORY_SEPARATOR . 'cache' . DIRECTORY_SEPARATOR;
  }
 
  /**
   * Sets the cache file name
   * 
   * @access public
   * @param string $file Name of the file to write and read the cache
   * @param boolean $useCachePath If true, will prepend the file name with the 
   *                              cache path
   */
  public function setFile($file, $useCachePath = false) {
    // If there is a file name given...
    if ($file) {
      // Prepend the path if it is being asked for
      if ($useCachePath) {
        $file = $this->_cachePath . $file;
      }
 
      // Set the name
      $this->_cacheFile = $file;
    } else {
      // Build a name
      $this->_cacheFile = $this->_cachePath . get_class($this) . '-cache-file.php';
    }
  }
 
  /**
   * Sets the cache file expiration time duration
   * 
   * @access public
   * @param int $expiry Time in seconds to keep the cache before dumping and 
   *                    starting over
   */
  public function setExpiry($expiry) {
    if ($expiry !== null) {
      $this->_cacheExpiry = intval($cacheExpiry);
    }
  }
 
  /**
   * Gets the current contents if there is one
   * 
   * @access public
   * @return mixed Null if there is none, otherwise whatever is in the cache
   */
  public function get() {
    // See if we need to make the temp file
    if (!file_exists($this->_cacheFile)) {
      // This will simply make an empty file for writing. Maybe.
      if (!touch($this->_cacheFile)) {
        $this->_cacheFileWritable = false;
      }
 
      // Send back something to be used by the child
      return null;
    } else {
      // Get the cached data
      return include_once $this->_cacheFile;
    }
  }
 
  /**
   * Writes data to the cache file
   * 
   * @access public 
   * @param string $data The contents of the cache file as a string
   */
  public function put($data) {
    // Write it now if we can
    if ($this->_cacheFileWritable) {
      $fh = fopen($this->_cacheFile, 'w');
      fwrite($fh, $data);
      fclose($fh);
    }
  }
}
 
// Simple sample implementation
class SlowQueryCache extends SmallCache {
  public function fetch() {
    // Get the cache data
    $filedata = $this->get();
 
    // Now see if we have a cache and if the cache is young enough, if not, make the cache and use the data now
    if ( ($filedata && $this->_currentTime - $filedata['timestamp'] > $this->_cacheExpiry) || !$filedata) {
      // Set the query - pretend it is a MySQL query
      $sql = 'call some_proc_that_might_take_long()';
 
      /** 
       * Get the result set
       * 
       * In our case we are pretending to use PDO - substitute your own DB
       * handler code here
       * 
       * We are also pretending to get our DB handle from a registry in 
       * our library
       */
      $db = Libregistry::getInstance()->get('objectdbmysql');
 
      /** 
       * Call the query
       * 
       * I am leaving error handling up to you.
       */
      $rs = $db->query($sql);
 
      // Fileinfo is what is going to go into the file
      $fileinfo  = "<?php\n";
      $fileinfo .= "\$data = array(\n\t'timestamp' => $this->_currentTime,\n\t'rows' => array(\n";
 
      // File data is what would normally come from the cache
      $filedata['timestamp'] = $this->_currentTime;
      $filedata['rows'] = array(); // Clears out any old crap
 
      /**
       * Loop and build
       * 
       * This will be entirely dependent on your data structure and how 
       * you expect to access the data from within the cache
       */ 
      while ($row = $rs->fetch()) {
        $fileinfo .= "\t\t'$row[id]' => array('name' => '$row[name]', 'age' => '$row[age]'),\n";
        $filedata['rows'][$row['id']] = array('name' => $row['name'], 'age' => $row['age']);
      }
      $fileinfo .= "\t),\n);\nreturn \$data;";
 
      // Write it now if we can
      $this->put($fileinfo);
    }
 
    // Send back the data we have
    return $filedata;
  }
}
 
// Tests
$cache = new SlowQueryCache('cache/slowquery.php');
$data = $cache->fetch();
var_dump($data);
?>

Basically what this does is check to see if there is a cache file. If there is, it gets it and reads it and looks at the time written into the cache file to compare the current time minus the cache time against the expiry limit. If the cache is expired, the data is collected from the database, a result array is build and the result array is then written, as a string of PHP code to the cache file while the array data is returned.

If the cache has not yet expired then the original cache data is returned.

Keep in mind that this is still very tailored to my personal needs. But it should be easily enough transitioned to something more useful with a little tweaking. And yes, I know that this can be done in a better, more robust and sexier way. But this worked for me and I figured I'd share it in case it might work for you.

12Mar/094

Coding clean should be a lifestyle

Dear rookie PHP coders/script kiddies/n00Bs: if you plan to use a variable or array index please remember to initialize them first or I will hunt you down and gut you like a pig. Kthnxbai.

I am in the middle of a redevelopment project right now that is pissing me off beyond imagination. The main reason is because the original developer of the codebase decided that initializing variables, checking existence of variables and checking existence of common array indexes was not necessary. And it got me thinking... coding clean should not just be a high priority, it should be a way of life for developers.

For those of you that may just be getting started in the big scary world of PHP development I want to offer a suggestion: before you even think of using a variable or array index either initialize it or check its existence.

What is initializing a variable or array index you ask? Simple. Declare it with a value. Like this:

<?php
// Initialize a variable
$myVar = null;
 
// Initialize a variable as an array
$myArray = array();
 
// Initialize an index
$myArray['myIndex'] = null;
?>

That isn't that hard, is it? Now lets that this a step further and make sure that before we use something it is actually available to us, shall we?

<?php
if (isset($myVar)) {
  // Do something based on $myVar being set
}
 
if (empty($myArray)) {
  // Do something based on $myArray being either not 
  // set or set to a value equal to (boolean) false
}
?>

Using empty() and isset() are two things you can do before using a variable. This allows you to not only check if a variable has been set but also allows you to check a specific empty value.

Something to take note of here is that isset() will still return false if you check a variable that has be assigned a value of 'null'. However, because it has been declared you can use the variable in conditionals and other expressions without worry of generating an undefined variable notice.

<?php
$myVar = null;
 
if (!$myVar) {
  // So something if $myVar evaluates to (boolean) false
  // And you don't have to worry about throwing errors now
}
?>

If you plan on using a constant, which is not a variable, then you would use defined().

<?php
define('ROBERTROCKS', true);
 
if (defined('ROBERTROCKS') && ROBERTROCKS) {
  echo 'Man, Robert must rock';
}
?>

Moving into an area that is a tad more useful, there are times when your code is going to enter a loop or other construct (like a switch for example) and something done inside of that construct will set a variable that you will use after the construct closes. When this happens, make sure to initialize your var outside the construct so that if the construct never opens or is never entered you can still use the variable after the construct closes.

<?php
/**
 * Make sure we have clean access to the variable 
 * regardless of whether the construct is used
 */
$myVar = '';
 
while (loopRuns()) {
  $myVar .= getLoopVal();
}
 
/** 
 * Since we made this variable before entering
 * the construct we can be sure we can use it 
 * without issue even if the loop never happens.
 */
echo $myVar;
?>

So to close this out let me point out a few things that you should never see in your code. Ever.

<?php
/**
 * This is crap. Don't ever do this. If a request is made
 * to this page without a querystring then $_GET['id']
 * will not be set and you will look like an ass for not
 * knowing how to code cleanly.
 */
$id = $_GET['id'];
 
/**
 * This is another stupid. How do you know if the session
 * variable has been set? You don't, and if it isn't set you
 * will be notified.
 */
if ($_SESSION['hasVar']) $hasSessionVar = $_SESSION['hasVar'];
 
/**
 * Last example of what not to do... never build a var
 * inside a construct for use outside the construct without
 * initializing it.
 */
switch ($myVar) {
  case 'crap':
    $newVar = 'This is crap';
    break;
 
  case 'bull':
    $newVar = 'This is bull';
    break;
}
 
echo $newVar;
?>

I hope this helps someone learn to code a little cleaner. And perhaps save you all sorts of curses and negativity by developers that might have to try to work on your code long after you stopped working on it.