Closures are coming to PHP

Dagfinn has a post looking at using the new closure feature of PHP 5.3. He compares using foreach for iteration versus array_map. “Interesting,” he concludes, “but not necessarily better than conventional alternatives.”

I agree for that case. Consider instead, a more complicated operation that requires a setup and a tear down after.


Now what happens if we need to be able to customize operation? That’s common enough, one way of doing this is to create a template method.

class MyExample {
    function operation() {}
    function setup() {}
    function teardown() {}
    function doit() {

Now, we can subclass MyExample and override operation() with custom logic. This is well and good, but what the customization we need is fairly small. Creating a new class carries a certain weight. Especially if you are religious about one class per file.

class MyExampleExtension extends MyExample {
    function operation() {
        // custom logic

Plus, you now have to deal with some creational patterns to make sure your custom class is used in the right context.

$myObject = $registery->get('MyExample');

So, instead of encapsulating the pattern, its also very common to just copy and paste:


But that’s not good on the duplicate code front. So here is an alternate implementation, but using a closure anonymous function as a callback.

class MyExample2 {
    function setup() {}
    function teardown() {}
    function doit($operation) {

The advantage of MyExample2 is that extending is that the setup and teardown pattern is encapsulated in one spot. You konw that if setup is called, teardown will also be called. But, extending the operation is very light weight.

$myObject = new MyExample2();
$myObject->doIt(function () { /* custom logic 1 */ });
// ...
$myObject->doIt(function () { /* custom logic 2 */ });

There is another significant benefit to this and that is locality of reference. Here, the custom1 logic and the custom2 logic appears in context, not far away in some custom class or function declaration. So you get encapsulation and reuse for the common code parts, but without the sprawl and overhead of declaring structures that will only be used once in a context far away their declaration.

Closures and anonymous functions decrease the activation energy to write good code.

That’s not to say that closures and anonymous functions can’t be abused. If you keep seeing the same logic over and over in an anonymous block, you should probably give it a name in the form of a class, method or function.

Holiday Tech Support

I don’t see my family as often since I’ve moved to San Francisco. This weekend I’m home for the Easter holiday. Its nice to see everyone. Additionally, I have a backlog of tech support for my parents and grandparents. I’ve installed software purchased months ago, done updates that they weren’t even are of, installed a router and fixed rats nests of cables. I’ve restored TV setups to working order and am about to fix a vacuum cleaner. Are you the tech support guy for your family? What are you fixing this weekend?

Sarah Snow Stever


I am very sad. Two weeks ago, my cousin Sarah had a stroke and died. She was 35, two years younger than me.

As kids, Sarah and I, (along with her sister Rachel) would spend weeks in the summer staying at my grandparents house, playing and doing the things that ten year olds do on a farm. We sat around the campfire at family reunions. We played cards and games, talked and argued. I always looked forward to seeing all my cousins at holidays and family gatherings, but Sarah and Rachel were special then because they were closer to my age.

As adults, Sarah and I also did stuff together on occasion. We still sat around the campfire at the family reunions and visited during the holidays. But, we also went to bars and restaurants, Sarah always knew the best bars. We went to Cedar Point and shared an automobile accident. She would cut my hair and I would fix her computer. But mostly, we just talked. Sarah was just plain easy to talk to and always interesting.

In recent years Sarah moved to Atlanta to build a life for herself there. She opened a salon there and infused it with her character and personality. It was a place where she was at home and happy. I’m sure her clients felt happy and at home there as well. (A client remarks on Sarah’s passing)

But, the most important thing about her move to Atlanta was meeting her husband, Kevin there. I’ve only met Kevin a few times, but the one thing that I know about him is that he made Sarah happy.

I haven’t seen Sarah as much in the last few years. Atlanta is far from Michigan and she disliked flying. She came to fewer and fewer holiday functions. Despite her many invitations to visit Atlanta, I didn’t go.

That is until September, when I went to the php|works conference in Atlanta. One of the reasons I wanted to go to the conference was to be able to see Sarah. After the conference, I stayed with her for a couple days.

Sarah showed me her Salon and I could see how much she loved it. She introduced me to the dogs that she saved. We went out to eat and visited the local Atlanta attractions. But mostly, we talked. We talked about family, dating, kids and careers. We talked about her writing, the gym she liked, the church she had joined and the things she wanted to do.

Sarah tried very hard to convince me to move to Atlanta. I think she felt that all I needed to do was to move there and I would meet the love of my life and l could live there happily to the end of my days. After all, she did.

There is so much that I still want to do with Sarah. I feel like I’ve always taken it for granted that that she would be around for us to “do that later.” I guess not. I’ll miss Sarah.

Sarah’s obituary.

Benchmarking PHP’s Magic Methods

Larry Garfield has an interesting set of benchmarks covering many of PHP’s magic methods. His results correspond pretty well to my own benchmarks in the area. The thing to take away is that its not necessarily the overhead of the magic methods, but rather what you do inside them. Its hard to do anything useful inside a magic method, such as __get or __call that isn’t 10 to 20 times slower than the “non-magic” solution.

There are probably more than a few naive programmers who would read results like this and start to avoid these constructs in their own code for performance reasons. These are the same kinds of people obsessing over a few single or double quotes, who eschew “slow” objects in favor of switch statements that are many times slower than the polymorphic method calls they are trying to avoid.

But, that’s not the end of the story. Larry ran his benchmarks using 2,000,000 iterations. The N really matters here. Sure, iterators are slower than arrays, but you aren’t going to be iterating over two million things. I tend to fetch my database records in lots of 25 or 50. You aren’t going to be making two million invocations of __call. But how many will you make? Under what value of N does the performance of these techniques cease to matter? Is it ten, one hundred, one thousand, or ten thousand? You may be surprised at how few calls your program actually does and how little impact it has on performance.

As Wez and Travis point out in their comments, profiling is the way to find out the potential impact and to discover your true N.

Paul M. Jones has a good example of what I’m talking about. There, call_user_func_array appears to be a bottleneck, but it turns out that its the function being called, htmlspecialchars, not the calling process that consumes the balance of the time. In that case, the function was “only” called 300 times. I find that order of magnitude to be fairly typical. Something to be aware of, perhaps, but not something to obsess over.

Working with PHP 5 in Mac OS X 10.5 (Leopard)

PHPMac OS X is a great development platform for working with PHP. Leopard comes with Apache, PHP and many other development tools, such as subversion already installed. Leopard brings a much needed upgrade from Tiger’s tired PHP 4 to a very modern version of PHP 5.2.4. This is a guide for setting up a PHP development environment under 10.5 using the version of PHP that ships with leopard.

You may prefer to use one of the 3rd party distributions of PHP, such as MAMP, XAMPP or Marc Liyanage. This is a guide to using the version of PHP that comes with 10.5.

Enable Developer Tools

These steps may not be strictly necessary for this process, but I find it useful to do them.
First, enable your root password.
You may also want to install XCode Tools from your Leopard disk (or grab the latest from Apple developer tools). The tools are required is you are going to compile any extensions for PHP.

Editing Configuration Files

We will have to edit several configuration files that exist as part of the unixy underpinnings of OS X. I’m going to recommend the free text editor, TextWrangler for this purpose. Normally, the finder hides the configuration files from view. However, in the finder, you can use the “Goto Folder…” option under the “Go” menu to view these files. This option if available via command-shift-G. Actually, this option is available in any file open dialog in OS X via command-shift-G. In addition, Text Wrangler will allow you to browse these files with its “open hidden…” option. But, the much easier option is selecting “Open file by name…” (command-D) and just typing the full path and filename. To save many of these files, you will need to enter your root password. Be Careful.

Enabling PHP

PHP is installed in Mac OS X by default, but not enabled. To enable it, we must edit the apache 2 configuration file, which is located at /etc/apache2/httpd.conf. Find the line which loads the PHP 5 module, which looks like this:

#LoadModule php5_module libexec/apache2/

The line is currently commented out. All we have to do is remove the comment symbol, #, so the line looks like this:

LoadModule php5_module libexec/apache2/


Starting Apache

Go to the sharing panel in system preferences and enable “Web Sharing.” This will start the apache server.
Sharing Panel
Another way to do this is to type the following in the Terminal application:

sudo apachectl start

You will be prompted to enter your root password. After that, your apache server should now be running. If you need to restart the server from the terminal, you can type this:

sudo apachectl restart

If you find this tedious to type, there is a script that you can download to do this later in this post.

Visiting our Web Site

Now, lets check our work. In the sharing panel, you can click on the URL under “Your computer’s website.” Alternatively, in the web browser, go to the url http://localhost/. localhost is a special name that means “My computer.” If your web server is working, you should see a page titled “Test Page for Apache Installation.” If you go to http://localhost/manual/, you can read an Apache 2.2 manual, hosted from your own server. But, this don’t tell you that PHP is working.
For that, we’ll have to create a very simple php program. Create a new file in TextWrangler and type the following:

< ?php phpinfo(); ?>

(Don’t just copy and paste this. Note that there should be no space between the < and the ?php. The WordPress software I use for this blog inserts an extra space.)
Save this using the file name info.php in the /Library/WebServer/Documents/ directory. (start from the top level directory of your hard drive, not the library directory in your home directory. Now you should be able to visit the PHP page you just created by visiting http://localhost/info.php. You should see the PHP logo and a big table of configuration information.

Showing the World

For security purposes, you should consider that anything you put in your WebServer/Documents folder will be available across the web. If you have information that you want to keep private, think twice about putting it there, unless you know how to protect it.
But, if you want people to see the pages that you are sharing, there can be a few obstacles. You can give out the URL that is listed in the sharing control panel under “Your computer’s website.” However, if you are behind a NAT router, such as I am, this IP address based url will only work for other computers on your network and not for the internet as a whole. You may have to configure network router or firewall in order to discover your true ip address and to route web server requests to that IP to your computer. Doing this is beyond the scope of this tutorial.
Additionally, IP address based urls don’t make good urls to share. IP addresses can change. If you plan to host a permanent web site, you may want to purchase a domain name and point it to your Mac. This also, is beyond the scope of this tutorial.
Perhaps the best option is to purchase both a domain name and professional hosting. Apache based PHP Hosting is widely available and cheap. You can get support from a good host on uploading your files to the remote server. I’m going to presume that you will use one of the many excellent PHP hosting options and are only configuring PHP on your own machine for education, testing or development purposes.

Enabling a Personal Website

If you clicked on the URL under “Your Personal Website,” you might have gotten a page that says forbidden. This is because in the default configuration in Leopard, unlike in Tiger, does not allow Apache to serve documents from home directories. If you want to enable this feature, you have to create a new Configuration file.
Create a new file with the following contents and save it to /etc/apache2/users/jeff.conf.

<directory "/Users/jeff/Sites">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all

Replace “jeff” with your user name, which is also the name of your home directory. Exact capitalization is imporant. This tells the Apache server that it is ok to serve web content out of the ~jeff directory. You will have to restart Apache for this to take effect.
You may also have to create a Sites folder in your home directory to hold the files you want to serve. Leopard will automatically bless this folder with a special Icon.

Virtual Hosting

If you want to experiment with or work on more than one site at a time, the single directory in WebServer Documents and the Personal Websites configuration don’t work well. Projects collide and files outside of your home directory can be harder to work with. The answer to this is to setup virtual hosting. Lets turn our Personal Website sharing solution into a virtual hosting solution that allows us to work with multiple websites as subdirectories of our Sites folder.
So, lets create a sample site, called mysite. We’ll create a folder called “mysite” as a sub folder of our Sites folder. Capitalization is important.
Now, we are going to want to access our site with an easy to use domain name, so that our url is http://mysite/. There is an easy way to create new domain names that are only for personal use. To do this, we can add it to our /etc/hosts file. Add the following lines at the end of this file:

# My local aliases mysite is a special IP address designation that never changes and corresponds to localhost to mean this computer. We are telling our Mac that the name mysite is hosted on the local computer. This rule is only in effect on the same machine. If you go to a different machine, you cannot use the http://mysite/ url.
Now we need to configure apache for virtual hosting. We are going to have to edit our /etc/apache2/users/jeff.conf file. Change the contents of this file to the following:

<directory "/Users/jeff/Sites/*/">
    Options Indexes MultiViews FollowSymLinks
    AllowOverride All
    Order allow,deny
    Allow from all
NameVirtualHost *:80
<virtualhost *:80>
    DocumentRoot /Users/jeff/Sites/mysite
    ServerName mysite

Remember to replace “jeff” with your user name. Place your info.php test file into the mysite directory and rename it to index.php. Now, restart your apache server. When you visit http://mysite/, you should now see the familiar php logo and information page.
If you want to add another site, just add a second line in your hosts file, another subdirectory of Sites and append the following to your apache configuration file:

<virtualhost *:80>
    DocumentRoot /Users/jeff/Sites/myothersite
    ServerName myothersite

Sharing with the World, Part II

Sharing your virtual hosted sites with the world is more complicated if you don’t have a domain name setup. You can, however, add your hosts files entries to other computers that you want to share with. However, you have to change the IP address to the IP address of your computer, taking into account any NAT.
There is a special case of this. If you are using parallels, perhaps for test viewing your pages in internet explorer, you may want your virtual hosted sites to be available. The good news is that Windows also supports a hosts file. Here is how to edit your windows hosts file. The big problem is knowing what IP address to use. You can’t use on the windows side because that is the virtual windows machine, not your Mac’s address. You can use the IP address shown on your network system preferences panel, for me. But, this number is subject to change and you will have to re-edit your hosts file on the windows side.
If you are using Parellels, be sure to upgrade to the new beta version for Leopard, build 5540. Once you’ve done that, if you visit the network panel in system preferences and select the “Parallels Host-Guest” network, you will see the IP address that parallels assigns to your host machine. (assuming you are using Shared Networking.) You can then use this IP address in your windows hosts file. You may also be able to change “Using DHCP” to “Using DHCP with Manual address” and re-entering this number if you have a problem with the number changing. Here, my number is

Network Preferences panel

Installing MySQL

MySQL has a binary distribution for Mac OS X. They also have reasonably good documentation on installing MySQL on Mac OS X for their distribution. Note that Leopard specific packages for MySQL have not been created yet.

Starting MySQL

So far, the MySQL preferences panel from the Tiger release is broken and does not correctly start and stop MySQL (bug report. You can do this from the terminal window with

sudo /usr/local/mysql/support-files/mysql.server start

To shutdown the server type:

sudo /usr/local/mysql/support-files/mysql.server stop

If you find this tedious to type, you can download WebDevCP, which is a small AppleScript application that I made. Launching WebDevCP launches both Apache and MySQL. Quitting the application shuts them both down. usually. Launching and quitting requires a password. No warranty on this thing. It was just something I was using personally and figured others might find useful.

Bring the mysql.sock to PHP

One problem that has come about with MySQL and Leopard is the location of the mysql.sock file. Previously, the default location for this file was in the /tmp directory. That location has now moved to the /var/mysql directory. PHP will look for it there. Unfortunately, the default location from the MySQL will still place it in the old location. We can fix this by creating a my.cnf configuration file in the /etc directory. Save a file with the following contents to /etc/my.cnf:

socket = /var/mysql/mysql.sock

socket = /var/mysql/mysql.sock

In the terminal window, type the following commands to create the directory for the sock file:

sudo mkdir /var/mysql
sudo chown _mysql /var/mysql

One drawback to this is that if you have installed the MySQL GUI tools, they will look for the mysql.sock file at the old location. You can enter the new socket in the connection dialog under More Options, there is a box labeled “connect using socket.” Just enter /var/mysql/mysql.sock.
Another solution is to change the php.ini file to expect the socket in a different location. I’m going with the my.cnf option because I expect the MySQL will have a Leopard version out in a few days that changes the default location.

Where is PEAR?

OS X has traditionally had problems with PEAR. Many point updates would overwrite the included version of PEAR with an older, and perhaps insecure version. Sadly, Apple has fixed this by not including PEAR at all in their OS. This is a big inconvenience for people wanting to use Apple’s default version of PHP, versus a third party distribution. So, lets get PEAR installed. Type the following in the terminal window to download the PEAR installer:

curl > go-pear.php

after that, type

sudo php -q go-pear.php

To run it. Hit enter to select the default locations. PEAR will be installed, but it won’t be ready to use until we modify our php.ini file.

PHP .ini configuration

Now we need to make some changes to our php configuration file. Leopard has an empty configuration file by default, but provides a file which you can use as a template. From the terminal window, type:

sudo cp /etc/php.ini.default /etc/php.ini

Now, edit the /etc/php.ini file. Find the include_path setting:

;include_path = ".:/php/includes"

And change it to

include_path = ".:/usr/share/pear"

This enables our PEAR installation. You may also want to make some changes which will improve your ability to debug PHP. FInd the line that says

log_errors = Off

and change it to

log_errors = On

You have to then restart Apache for these PHP changes to go into effect.

Errors and Omissions

Thats all there is to using the version of PHP delivered with OS X. If you find this confusing, you are probably better off with something like XAMPP or MAMP. I’ll probably end up compiling my own versions of PHP, but that is a different blog post. I’ve already had problems with this configuration when I tried to install XDebug via PECL. One last thing, if you run into problems, you can check the apache2 error_log file using the Console application.

For support, try the Sitepoint forums or Apple’s Discussion Forums.