PHP is so Lazy!

Posted by paul on October 30, 2008.

Last week I started reading Extending and Embedding PHP. I really like extending scripting languages for a number of reasons, most of all because it's a great way to learn about the more esoteric features of a language. So far I've discovered two neat things about PHP that made me smile, so I thought I'd share them here.

Copy on Write

In an effort to save CPU cycles, PHP uses an optimization strategy called "Copy on Write". Basically what this means is that variable data isn't copied until it needs to be. Using an example similar to the one found in the book:

$a = 50;
$b = $a;
$b += 5;

At the first line, the variable $a is created and memory is allocated for it. When we set $b to equal $a however, you might think that the data in $a is copied to $b. Fortunately PHP is smarter than that and doesn't actually copy the data until the 3rd line when $b is incremented by 5. The value in this is that data is not copied unnecessarily. If, for some reason, we took out the 3rd line and $b was never modified after the initial assignment, the data would actually never have to be copied.

Return Value Used?

The second, admittedly more fabulous and intriguing thing I learned was that PHP provides internal functions a parameter called return_value_used whose value can be inspected to determine whether the return value being prepared by the function is actually being saved or not. Imagine you have a function that does some heavy crunching to compute a return value, and a caller (for some reason beyond us) does this:

<?php
...
myextension_compute_something();
...
?>

In other words, they call the function but don't store the return value. Well, being a thoughtful internal function author, you can check to see that return_value_used evaluates to false and just skip doing any work, saving CPU cycles and time, like so:

PHP_FUNCTION(myextension_compute_something)
{
    if (return_value_used) {
        // some fancy but expensive algorithm
        // ...
        RETURN_DOUBLE(some_value);
    } else {
        RETURN_NULL();
    }
}

Clever!

ZendCon 2008 - Day 2

Posted by paul on September 18, 2008.

Harold Goldberg, the CEO of Zend Technologies, started the day off with a talk called "Insights from the Experts: How PHP Leaders Are Transforming High-Impact PHP Applications". The talk was basically a run-down of how PHP is doing out there. (Answer: Fairly well!). There were a few case studies of larger companies using PHP (and of course Zend products) and a few reports / surveys / etc. Nothing particularly groundbreaking but it was good to start the day feeling pumped about being a PHP programmer...

Later on I attended "Tiery-Eyed", a talk given by a Zend employee, Kevin Schroeder on ... you guessed it, tiered application development. It was pretty decent, Kevin walked the audience through the creation of a micro-blogging application using a variety of different backend tiers. I left with an urge to create the smallest application I could think off with one SOAP backend, one XML-RPC backend and one REST backend.

The highlight of the day was Sara Goleman's talk on PHP Extension Writing. This was the most in-depth of the talks I attended and despite a few technical problems, the talk went smoothly. I'm now motivated to actually read more than the first 25 pages of her book on the subject.

The day ended with a reception in the exhibit hall. Free drinks and food is never a bad way to end a day...

Using YAML with the Zend Framework

Posted by paul on February 18, 2008.

One of the many great components provided by the Zend Framework is Zend_Config. In a nutshell, this component allows you to access configuration data (from a file, array, etc) through a nested object property based interface. Out of the box, the component supports working with XML and INI files via the Zend_Config_Ini and Zend_Config_Xml Config adapters.

If you're familiar with frameworks like Ruby on Rails or Symfony however, you may notice that YAML support is missing. This is mostly because there is no one YAML parsing library for PHP. If you are willing to introduce another dependency to your application however, there is a really easy way to use Spyc to bridge YAML and Zend_Config. One of the nice things about the Zend_Config component is that it can take data right out of a PHP array (as opposed to an XML or INI file). This gives us quite a lot of flexibility, especially considering that Spyc returns parsed YAML data as a PHP array. Armed with this knowledge, the solution becomes really, really simple:

<?php
require_once 'Zend/Config.php';
require_once 'spyc.php';

$configFile = "config.yml";
$config = new Zend_Config(Spyc::YAMLLoad($confFile));
?>

Remarkably simple isn't it? Now, given the YAML config file:

 webhost: www.example.com
 database: 
     adapter: pdo_mysql
     host: db.example.com
     username: dbuser
     password: secret
     dbname: mydatabase

The above PHP code will create a config object that can then be used like this:

<?php
print $config->webhost;        // prints 'www.example.com'
print $config->database->host; // prints 'db.example.com'
?>

Letting the masses decide...

Posted by paul on January 8, 2008.

Sho Kuwamoto has been doing some excellent work on implementing improved pluralization support for PHP, Ruby on Rails and AppleScript. When I put together a quick solution for this in PHP, I couldn't help but think that there might be some missing special cases or what have you. Well, Sho has now put together a feeder app for the pluralizer. What a neat experiment in collaborative web production! Go on and give it a try.

Perforce Knowledge Base Launched

Posted by paul on November 4, 2007.

In case you missed the announcement, the Perforce Knowledge Base is now available. Click here for more information about the technology driving the site.