November, 2006

Nov 06

Prototype extensions

Prototype may be ubiquitous, but there’s some functionality it has yet to cover. Here are a couple of useful extensions I’ve run across lately to fill in the gaps:

Cookie, by Carlos Reche

JavaScript already does a pretty good job of getting and setting cookie values on its own, but this extension makes it trivial. The simple Cookie object gives you access to get(), set(), erase(), and accept() methods.

var Cookie = {
  set: function(name, value, daysToExpire) {
    var expire = '';
    if (daysToExpire != undefined) {
      var d = new Date();
      d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
      expire = '; expires=' + d.toGMTString();
    return (document.cookie = escape(name) + '=' + escape(value || '') + expire);
  get: function(name) {
    var cookie = document.cookie.match(new RegExp('(^|;)s*' + escape(name) + '=([^;s]*)'));
    return (cookie ? unescape(cookie[2]) : null);
  erase: function(name) {
    var cookie = Cookie.get(name) || true;
    Cookie.set(name, '', -1);
    return cookie;
  accept: function() {
    if (typeof navigator.cookieEnabled == 'boolean') {
      return navigator.cookieEnabled;
    Cookie.set('_test', '1');
    return (Cookie.erase('_test') === '1');

Event.wheel(e), by Frank Monnerjahn

This tiny extension (about a dozen lines) adds support for mouse wheel events—handy for any number of things, like manipulating a gauge or slider, or scrolling something sideways. Frank based this code on an example by Adomas Paltanavičius.

Object.extend(Event, {
  wheel:function (event) {
    var delta = 0;
    if (!event) {
      event = window.event;
    if (event.wheelDelta) {
      delta = event.wheelDelta/120;
      if (window.opera) {
        delta = -delta;
    } else if (event.detail) {
      delta = -event.detail/3;
    return Math.round(delta); //Safari Round

And a usage example:

Event.observe(document, 'mousewheel', yourCallbackFunction, false);
Event.observe(document, 'DOMMouseScroll', yourCallbackFunction, false); // Firefox
Like this post? You might also like Coalmine, my centralized error tracking service for your apps. Coalmine captures errors and all kinds of helpful debugging information, notifies you, and makes it all searchable. Check it out!

Nov 06

Faster JavaScript in Firefox 3, thanks to Adobe

You might have seen the press release from Mozilla in the news today.

SAN FRANCISCO November 7, 2006 Adobe Systems Incorporated (Nasdaq:ADBE) and the Mozilla Foundation, a public-benefit organization dedicated to promoting choice and innovation on the Internet, today announced that Adobe has contributed source code for the ActionScript™ Virtual Machine, the powerful standards-based scripting language engine in Adobe® Flash® Player, to the Mozilla Foundation. Mozilla will host a new open source project, called Tamarin, to accelerate the development of this standards-based approach for creating rich and engaging Web applications.

This is great news for developers and users alike. Essentially, Adobe has given away their JIT compiler for ActionScript to Mozilla, and Mozilla has named this “Tamarin” (à la SpiderMonkey). This isn’t Flash—just the component of Flash that compiles ActionScript within Flash.

Because both ActionScript and JavaScript are based on the ECMAScript specification, this means that Mozilla will be able to use this compiler to significantly speed up JavaScript functionality in Firefox 3. Since that includes XMLHttpRequest, the functionality driving so-called Web 2.0 applications, it means that developers can get more and more daring with their use of JavaScript-heavy websites, allowing users to do more and do it more quickly, with fewer page refreshes. It also means Firefox plugins will be faster. Firefox 3 is expected to be released in early- to mid-2007.

This may not sound like a big deal, but consider this: Adobe is contributing around 135,000 lines of code to Mozilla. That’s the single largest contribution to the Mozilla Foundation since Netscape originally made Mozilla open source in the first place.

Frank Hecker (executive director of Mozilla) has a great write-up on his website explaining today’s events in more detail, so if you want to know more, I encourage you to take a look.

Like this post? You might also like Coalmine, my centralized error tracking service for your apps. Coalmine captures errors and all kinds of helpful debugging information, notifies you, and makes it all searchable. Check it out!

Nov 06

Zend/PHP Conference wrap-up

It was a fun week. Of course, the big news on Tuesday was Zend’s new partnership with Microsoft, which promises to make PHP perform better with Windows in general, and IIS and Windows Server 2003 in particular. Zend’s own branch of PHP, Zend Core, will include even more improvements. Both Zend and Microsoft were obviously excited by the announcement, and I’ve got to admit that I was pretty impressed by the ease of configuring PHP in IIS 7. They showed some in-house benchmarks, but I’ll be interested to see some independent tests conducted when the final product ships (sometime next year?).

At some point during the same keynote, MySQL AB CEO Mårten Mickos jumped on stage for about seven seconds just to brag about what we already know—that MySQL is the most-used database with PHP. I wish he had spent a few more seconds, then, and explained why they clearly don’t bother to test their installations with PHP, since the default installation of MySQL prevents PHP from compiling if it also has OpenSSL (a somewhat common package).

In any event, the hot topics for this year were scalability and security, and there were lots of sessions on both. Eli White, the senior developer over at Digg, gave a great talk on scaling techniques (OpenOffice Impress format). George Schlossnagle, lead developer of APC and, frankly, entirely too many other PECL packages, followed up with his own excellent talk on scalability (PDF), which was naturally a bit more focused on caching.

Jaisen Mathai of FotoFlix briefly covered JSON and PHP, although (as with all of the sessions) the time constraints really limited how much he was able to talk about. After the session I mentioned Zend_Json to him—although any JSON library will do for encoding and decoding objects, I like the things attached to that one in particular, like Zend_Json_Server. In one of my projects I decided to expose entire PHP objects as JavaScript, modify them on the client using some voodoo, then return back the entire object. That’s the kind of thing that’s really useful when you’re dealing with complex data and needing a simple UI that doesn’t require lots of page refreshing, and although his talk was a good introduction to JSON/PHP interaction in general it didn’t cover interesting ways that JSON can be used outside of retrieving exposed, external information or your standard Ajax stuff. And to be honest, I felt like the examples in his presentation would have worked better as REST services. Oh well, you can only do so much with 45 minutes, and he was a pretty good presenter notwithstanding.

Unit testing also got a deserving nod from Sebastian Bergmann, creator of PHPUnit. His talk hammered home the test-first methodology and also revealed some new functionality in the upcoming PHPUnit 3, including being able to automatically run Selenium tests. That’s hot.

I should also mention that Chris Anderson, author of The Long Tail, gave an excellent keynote on Wednesday. It wasn’t particularly tailored to apply to PHP, though, as one questioner pointed out. Still, I’m just as (if not more) interested in business strategy as I am in software development, so I loved it. Plus, we all got free hardcover copies of his new book, so that was nice (I picked up two—my boss wanted one). Maybe it’s just because he’s been touting the concept for awhile, but I was impressed at how on the ball he was with the Q&A session at the end.

Andrei Zmievski of Yahoo!, though, won the prize for having far and away the most interesting session of the week—”Unicoding with PHP” (PDF). He walked through in great detail the challenges of creating a Unicode-aware PHP 6 that (almost) transparently handles the hellish details of the standard. I’m guessing the Japanese user-base is drooling in anticipation of being able to (among other things) use hiragana, katakana, and kanji to represent variables instead of the Latin alphabet. Andrei promised a PHP 6 Unicode pre-release to get the functionality out there and tested, so keep an eye out for that. I’ll link to it when it’s available. Update: Here it is.

Last but not least, the Zend Framework get-together was one of the highlights of the week. Meeting Gavin, Darby, Bill, Andi, and all the rest of the Zend crew (along with contributors and users like Richard and Keith) in person was great, and I got a t-shirt. BONUS.

Anyway, that’s enough for this post, and I haven’t even covered half of it. Really, if you haven’t been before and you’re interested in PHP, you really should make some time to go next year. Plus, you know… t-shirts!

Like this post? You might also like Coalmine, my centralized error tracking service for your apps. Coalmine captures errors and all kinds of helpful debugging information, notifies you, and makes it all searchable. Check it out!