Blog Posts

Automating PHPUnit2 with SPL

I don't blog much any more. Much of what I work on any more is for my employer, Zend, and I don't feel at liberty to talk about it (and some of it is indeed confidential). However, I can say that I've been programming heavily on PHP5 the past few months, and had a chance to do some pretty fun stuff. Among the new things I've been able to play with are SPL and PHPUnit — and, recently, together.

Continue reading...

PHP error reporting for Perl users

On perlmonks today, a user was needing to maintain a PHP app, and wanted to know what the PHP equivalent of perl -wc script.pl was — specifically, they wanted to know how to run a PHP script from the commandline and have it display any warnings (ala perl's strict and warnings pragmas).

Unfortunately, there's not as simple a way to do this in PHP as in perl. Basically, you need to do the following:

  • To display errors:

    • In your php.ini file, set display_errors = On, or
    • In your script, add the line ini_set('display_errors', true);
  • To show notices, warnings, errors, deprecation notices:

    • In your php.ini file, set error_reporting = E_ALL | E_STRICT, or
    • In your script, add the line error_reporting(E_ALL | E_STRICT);

Alternatively, you can create a file with the lines:

<?php
error_reporting(E_ALL | E_STRICT);
ini_set('display_errors', true);

and then set the php.ini setting auto_prepend_file to the path to that file.

NOTE: do not do any of the above on a production system! PHP's error messages often reveal a lot about your applications, including file layout and potential vectors of attack. Turn display_errors off on production machines, set your error_reporting somewhat lower, and log_errors to a file so you can keep track of what's going on on your production system.

The second part of the question was how to run a PHP script on the command line. This is incredibly simple: php myscript.php. No different than any other scripting language.

You can get some good information by using some of the switches, though. -l turns the PHP interpreter into a linter, and can let you know if your code is well-formed (which doesn't necessarily preclude runtime or parse errors). -f will run the script through the parser, which can give you even more information. I typically bind these actions to keys in vim so I can check my work as I go.

If you plan on running your code solely on the commandline, add a shebang to the first line of your script: #!/path/to/php. Then make the script executable, and you're good to go. This is handy for cronjobs, or batch processing scripts.

All of this information is readily available in the PHP manual, and the commandline options are always available by passing the --help switch to the PHP executable. So, start testing your scripts already!

Continue reading...

Cgiapp dual releases

Today, I have released two versions of Cgiapp into the wild, Cgiapp 1.8.0 and Cgiapp2 2.0.0rc1.

Cgiapp 1.8.0 is a performance release. I did a complete code audit of the class, and did a number of changes to improve performance and fix some previously erratic behaviours. Additionally, I tested under both PHP4 and PHP5 to make sure that behaviour is the same in both environments.

However, Cgiapp 1.8.0 markes the last feature release of Cgiapp. I am deprecating the branch in favor of Cgiapp2.

Cgiapp2 is a PHP5-only version of Cgiapp. Some of the changes:

  • Cgiapp2 is an abstract class, with the abstract method setup(). Now it is truly non-instantiable!
  • Cgiapp2 makes extensive use of visibility operators. Key methods have been marked final, some methods are now protected, others static. See the changelog for more information.
  • Cgiapp2 is now E_STRICT compliant.
  • Cgiapp2 implements the CGI::Application 4.x series callback hook system. This is basically an observer pattern, allowing developers to register callbacks that execute at different locations in the runtime.
  • Cgiapp2 adds some extensive error and exception handling classes, including observable errors and exceptions.
  • I created a template interface. If implemented, a template engine can be plugged into the architecture at will — at the superclass, application class, and instance script level, allowing developers to mix-and-match template engines or choose whichever matches their taste, without having to rewrite application code. Three template plugins are included:

Cgiapp and Cgiapp2 are available at Sourceforge.

Keep reading for more information on the evolution of Cgiapp2.

Continue reading...

XP + Cygwin + coLinux == Productivity

I wrote earlier of my experiences using Windows XP, a move I've considered somewhat unfortunate but necessary. I've added a couple more tools to my toolbox since that have made the environment even better.

Continue reading...

Using Windows XP

Those of you who know me well know that I've been using Linux as my primary OS for many years now. At this point, for me, using Windows is like deciding I'm going to use a limited vocabulary; I can get the idea across, but not quite as well.

Due to the nature of where I work and the fact that I'm telecommuting, I've been having to maintain a dual-boot system. I use Ubuntu for my daily OS, and boot into Windows when I need to interact with people at work via Webex or Skype (we're using the new Skype beta with video, and it only works on Windows XP at this time).

This week, however, I've had to stay on Windows quite a bit — lots of impromptu conference calls and such. So, I've been customizing my environment, and been pretty pleased with the results.

Continue reading for some tips on customizing your Windows XP environment to work and feel a little more like… linux.

Continue reading...

Name recognition

Don't know if this is actually possible, but it appears Liam is already starting to associate names with people!

Just now, Jen was holding Liam, and turned him to face Maeve and I, who were sitting on the couch. He wasn't looking anywhere in particular. Then, Jen said, "Hi, Papa!" and his eyes moved to focus on me. A moment later, she said, "Hi, big sister!" and he moved is eyes to focus on Maeve.

As I type this, Jen just tried the experiment again with Maeve, and again he moved his eyes and head to look at her!

How cool and amazing it is to witness child development!

Continue reading...

Cgiapp 1.7.1 Released

I was able to roll a long-needed (and by some, long awaited) bugfix release of Cgiapp this morning. Cgiapp 1.7.1 corrects the following issues:

  • Cgiapp5::run() was corrected to call query() instead of cgiapp_get_query() (which caused a fatal error)
  • Cgiapp::__call() and Cgiapp5::__call() now report the name of the method called in errors when unable to find matching actions for that method.

As usual, downloads are available on my site as well as via SourceForge.

Update: The link on my site for downloading Cgiapp has been broken; I've now fixed it.

Continue reading...

Simple Caching for PHP

I ran across an article on "How to build a simple caching system, with PHP" on PHPit today. Overall, it's a fairly decent article, and uses some good principles (using the output buffer to capture content, using a callback to grab the captured content). There are a few minor improvements I'd make, however.

Continue reading...

Zend PHP Expo Presentation

Mike and I have just finished our talk on "Setting Up PHP". The number of attendees was N + 1, where N is the number of speakers… which was to be expected, as we were presenting opposite a session on web services, Shiflett's PHP Security talk, and a crash course on the ZCE. However, it's undoubtedly the best presentation missed by attendees. :-)

You can grab our presentation online.

Continue reading...

Review: php|architect's Guide to PHP Security

I flew in to San Jose today to visit Zend, and later attend the Zend/PHP Conference and Expo (two days left… register now if you haven't, and have the time to attend; the conference sessions promise to be very interesting).

During the flight, I had plenty of time to go through Ilia's Guide to PHP Security, which I'd ordered several weeks ago, but hadn't had time to read since.

Continue reading...