What drove me away from Apple

I recently replaced my Macbook Pro with a Thinkpad x200 running Arch Linux and Windows Vista. After 5 years of using Apple products I decided to switch – to borrow one of their marketing terms – because frankly, I was sick of being abused as a customer and the lock-in was making me less and less comfortable. Here’s a list of the things that really did it for me:

  • Pricing the Macbook Air so high with such low specs.
  • Releasing an pitiful update to the air along with a nice $100 price increase.
  • Doing away with the matte screen option.
  • Pulling the remotes from being included with laptops (admittedly not a huge deal, but felt like salt on a wound).

Is it just me or is Apple becoming a little too arrogant? Anyway, I know Lenovo won’t be saints either, but at least in the non-Mac OS X world I can easily switch vendors down the road if I ever need to.

PHP is Lazy

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. PHP 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 surprising 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();
    }
}

ZendCon 2008 – Day 2

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. It was pretty broad but still managed to be quite inspiring.

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

ZendCon 2008 – Day 1

Well yesterday was day one of ZendCon 2008. Actually, the conference proper started today, yesterday was just tutorials.

So far I’m pretty impressed. I attended the PHP Developer Best Practices tutorial in the morning and the Quality Assurance in PHP Projects tutorial in the afternoon.

The Best Practices tutorial was a little broad and most of it was focused on topics that were pretty common / basic. I’m not sure exactly what I wanted to get out of it, but for somebody who already uses an SCM system, who already tests (although not as much as I should) and writes api documentation it wasn’t the most informative tutorial.

Sebastian Bergmann’s QA tutorial was excellent. Of course, as the primary author of PHPUnit, I assumed Sebastian would know what he was talking about and I was right. He offered a really good, in depth look at PHPUnit and covered some of the newer features. He also covered Selenium and specifically Selenium RC in quite a bit of detail.

Looking forward to more sessions… the day of tutorials was a great start, can’t wait to see what the conference proper has to offer.

Using YAML with the Zend Framework

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'
?>