PHP with Apple's Developer Tools

Apple provides a powerful set of developer tools with Mac OS X. Originally developed by NeXT for programmers working with Objective-C, the tools have since evolved for use with a wide range of source languages, including Java, C++, and AppleScript. The Mac OS X developer tools package also includes the system header files and a C compiler, so it is an essential addition for anyone who wants a complete BSD Unix environment. At the heart of Mac OS X's developer tools is a full-featured text editor and IDE called Project Builder.

Though normally used with applications built with the Carbon and Cocoa frameworks, Project Builder is an excellent tool for Web developers working in languages like PHP and Perl. Along with a standard Apple graphical interface, it also provides emacs-style key bindings that will be familiar to Unix users: keys combinations like Ctrl-A or Ctrl-E can be used to move the cursor to the beginning or end of a line. Its syntax colorizer recognizes comments and quoted strings in a variety of languages, making source code easier to read. It is tightly integrated with CVS, a software management system commonly used in the open source community. When dealing with a large number of files, Project Builder allows the developer to sort the files into groups for easier navigation. It offers multiple editor windows, split views, and a powerful multi-file search-and-replace facility that can search for either plain text strings or a regular expressions.

Other tools bundled in the developer package include GNU Autoconf, both the BSD and GNU versions of make, and a useful application called FileMerge, which provides a graphical interface to the Unix diff utility. Project Builder uses FileMerge to display the differences between two versions of a file and allows developers to merge changes from the CVS repository into a new version. Some of the best features in the Developer Tools turn out to be of little use for Web scripting, though, such as the graphical Interface Builder and the excellent interaction between Project Builder and the gdb debugger.

I regularly use Project Builder and CVS to create and maintain complex PHP applications with dozens of source files. In this article, I'll illustrate some of the advantages of Project Builder and show some of the tricks I've learned.

Obtaining the Developer Tools

In the early 1990s, the developer release of NeXTSTEP sold for about $500 more than the standard user version. Today you can get the Developer Tools for little or no cost. The Developer Tools CD is included with most new Macs. Apple also gives the CD to new Apple Developer Connection members, and sells it for $20 from the ADC member's Web site at http://connect.apple.com/. If you have no CD, but a good Internet connection, you can choose to download the developer package from the ADC site; it's a hefty download, weighing in at 218MB.

As of this writing, the December 2001 release of the Developer Tools was the latest available version. For people running Mac OS X version 10.1 or higher, the oldest usable version is the September 2001 release; the previous (May 2001) release runs only on the original OS X 10.0.

Once you install the Developer Tools, Project Builder should be available under /Developer/Applications/, and you should be able to double-click on any file with a .c extension and have Project Builder open it. You could also create a new C source code file and open it from a Terminal window (using the open command) by typing:

touch file.c
open file.c

When you open a single file this way, it appears in Project Builder's editor. Project Builder's online help is available using the Command-? combination. You will most often use Project Builder with groups of files, or projects, and in order to see most of its features in action, you have to create a project.

Creating a Project

In order to have an example project to work with, you can retrieve a large Open Source PHP project from a CVS repository and then import its files into Project Builder. For this example I chose PEAR, a library of PHP classes available from http://pear.php.net, currently consisting of nearly 2,000 files. It takes a few shell commands to retrieve a working copy of PEAR. First you need to set the CVSROOT environment variable. If using the bash shell, issue this command:

CVSROOT=:pserver:cvsread@cvs.php.net:/repository

If using tcsh, use this:

setenv CVSROOT ":pserver:cvsread@cvs.php.net:/repository"

Then you can check out PEAR with the following commands.

cvs login
[enter password: anonymous]
cvs checkout pear

The cvs checkout command will fetch the PEAR code from the repository. Now, launch Project Builder and create a new project, like this:

Go to File -> New Project

In the New Project" assistant, choose Empty Project and then click Next

In the Project Name box, type PHP-pear

In the Location box, type in the name of the directory where you retrieved the CVS source and then click Finish

At this point, Project Builder's main window should appear. On the left you'll see the File and Groups window; on the right, an editor panel displaying some release notes. Above the text you will see four tabs labeled Find, Build, Run, and Debug. Of these, only the Find tab is useful for web developers; the rest are used to compile and test applications written in C or Objective-C.

Next, you can import all of the PEAR files into this new project with a single command.

Go to Project -> Add Files

In the column for the large pear directory, click on the top listing (Archive_Tar) and drag the mouse down the column until every file is highlighted. Then click Open.

In the next panel that appears, asking for a Reference Style, choose Group Relative.

Click Add.

The group relative reference style tells Project Builder to create a project structure that mimics the directory hierarchy, where each directory corresponds to a group. This is not the only way that Project Builder can use groups; a small C project might have one group for header files, one for C source files, and one for executables, even though all files are in a single directory. (For more documentation on creating and using groups see the Files topic in Project Builder's help.) Once you select Add, Project Builder will display all of the folders from the pear/ directory in its left panel. It will automatically see the project's CVS directories and enable CVS. Whenever you reveal a group, a small CVS Status... notice will appear in the bottom left corner of Project Builder's window as it queries the CVS server to check for updates on each of the group's files.