Though I have heard good things about Parallels and VirtualBox, I have always been a user of VMware. In particular, VMware Workstation. Workstation is great for firing up multiple Linux instances and testing out load-balancing or proxying scenarios. I haven’t really figured out any use for Windows VM’s other than testing IE6.
While there are a few Virtual PC hard disk images (.vhd) for Windows XP around, VMware cannot directly import .vhd files. It needs the actual Virtual PC virtual machine file (.vmc). After again losing my Windows XP virtual machine that I use for IE6 testing, I thought I’d document the process of running Windows XP in VMware so I don’t have to figure it out again the next time it happens.
Note: though these instructions are for VMware Workstation, some of this may apply to the free VMware Player.
So to use a class in PHP, usually, we first have to include the file that contains the definition for that class.
require('myclass.php');
$class = new MyClass;
But after you start instantiating a few classes here and there, problems arise.
require()
becomes big.require_once()
, which is inefficient.require()
that is needed, not in the current file, but in a file downstream, leading to a Fatal Error (if you use require()
and not include()
).The solution: __autoload()
The PHP autoload feature is one of the coolest features in the whole language. Basically, if PHP tries to load a class and cannot find the class definition, it will call the __autoload()
function that you provide giving it the name of the class it can’t find. At that point, you’re on your own. But how to find the file located on disk? There are four strategies for finding the right class file.
Zend_Auth_Storage_Session
class is defined in the file Zend/Auth/Storage/Session.php
. Autoload simply needs to replace the underscores for the system directory separator char and give it to require()
.At work, we use #4, naming conventions. Why? Well, why not? The other methods are heavier or more complicated, and I don’t see any gain. By using a simple naming convention, never again will we need to call require()
or include()
in our app. As a bonus side-effect, the code is organized in a consistent matter. Provided we follow the naming convention that Zend (and PEAR) use, our __autoload()
function looks like:
// by using PEAR-type naming conventions, autoload will always know where to
// find class definitions
function __autoload($class)
{
require(str_replace('_', DIRECTORY_SEPARATOR, $class) . '.php');
}
If we are to do unit testing, then it would be nice to have a simple way to run all the tests at once. And seeing the results in a command line is fine, but it would be nice to be able to generate some pretty reports I could view in a browser. And running coverage tools from the command line to see exactly what we’re testing is fine, but it would be nice to be able to generate coverage reports I could view in a browser. It would be nice to be able to do all these things with one simple command.
The command: phing.
If you’re familiar with Ant for Java, phing is functionally identical to ant. With a very simple build script, I can run all the unit tests, and generate test result reports and coverage reports in HTML automatically. Then I can fire up my browser and see if any tests failed, or whether we need new tests.
The following are examples of test results reports and coverage reports.
Phing currently requires PHPUnit for unit testing and xdebug for coverage. Someone would have to write a task to use other frameworks such as SimpleTest.
The phing build file is a first step towards automating builds with a build server. A build server can periodically check the project for changes. If new code exists, it will run tests and generate reports for tests, coverage, lint, anything. It can put all this on the web for easy inspection. It can keep versions of the whole project that be can be readily deployed. So if we have a staging and production server, and we stick to deploying ONLY bundles from the build server, it can make deployment and maintenance easier.