Building an Object Collection Manager with the SPL
Using gmdate()
19 August 2009 at 11:32
Filed Under: php
Recently at work I came across an odd bug involving gmdate(). A client wanted us to post some information to them including two fields: source_date and source_time. The values are really just the time when we're sending the information to them. However, they wanted it to be sent as GMT. So in preparing the request, I wrapped the source_time in gmdate(), tested and confirmed that it was giving the correct result. Then during an audit of their data they pointed out that around midnight our time would swing back 24 hours. Of course the issue was not wrapping the source_date in the same function. By not doing that our time would advance to tomorrow (from our perspective) but the date would be sent as today - therefore appearing to them as 24 hours previous. It's an easy bug to fix, and obvious once you think about it. I think it really shows the danger of failing to imagine edge cases - always a danger when working with dates.
Forking PHP
23 July 2009 at 16:10
Filed Under: coding, php
Interesting code used to fork long running processes in php.
The first script, prefork.php, is for forking a given function from a given file and running n children that will execute that function. There can be a startup function that is run before any forking begins and a shutdown function to run when all the children have died. The second script, prefork_class.php, uses a class with defined methods instead of relying on the command line for function names. This script has the added benefit of having functions that can be run just before each fork and after each fork. This allows the parent process to farm work out to each child by changing the variables that will be present when the child starts up. This is the script we use for managing our Gearman workers. We have a class that controls how many workers are started and what functions they provide. I may release a generic class that does that soon. Right now it is tied to our code library structure pretty tightly. We have also included two examples. They are simple, but do work to show you how the scripts work.from: http://brian.moonspot.net/php-fork
