[]RSS

[ Here: About | Archives+Tags | Artwork | Resumé | Contact ] [ Elsewhere: Comic | Projects | Philosophy | Work ]

Optional arguments in PHP

[Comment]

July 16th, 2008 in Micro Blog

Named, optional function arguments are a commonly used calling convention in Perl modules, used for constructors and heavily overloaded functions. They’re usually implemented with hashes in Perl, and while somewhat more work to iterate in the library than fixed arguments, they clearly document the call for the caller (and tend to be far less fragile).

They’re also easy to implement in PHP. Here’s an example (based on some CodeIgniter code I had laying around):

class bar extends Controller {

    protected $data = array();

    function foo($name, $extras = array('desc' => 'default1', 'on' => false)) {
        $this->data = array_merge($this->data, $extras);
    }

}
  1. The optional parameters are defined as the array elements of the $extra default array parameter value.
  2. The optional parameters are merged with the class data $this->data.

Calling the function is simple:

$b.foo('test', array('on' => true));
$b.foo('test 2' /* uses defaults */ );
$b.foo('test 3', array('desc' => 'Here is a description'));

The caller’s syntax isn’t quite as clean as Perl’s, but applying the defaults is cleaner (and merging the structures is about equivalent). Despite the slightly more complex calling convention, it’s still useful for setup functions with multiple optional parameters.

Client side logging for PHP

[Comment]

June 17th, 2008 in Links

PHP + Firebug == FirePHP. A simple API for logging to the Firebug console directly from PHP. Sound like fun?

Jay and Silent Bob on PHP

[Comment]

June 1st, 2008 in Links

Slides from php|tek 2008, including uber-geek Jay & Silent Bob references.

Inkscape 0.46 released

[Comment]

April 12th, 2008 in Micro Blog

decaying fujiI’ve had several hours to play with Inkscape 0.46 since it was released, and despite the minor version increment, a lot has changed.

New features

  • A “fill” tool, making it trivial to turn bitmaps into vectors quickly
  • 3d drawing shapes (including perspective)
  • A tweaking tool, making it easy to nudge/blur/soften shapes
  • Several new effects (including path effects)
  • Many performance improvements, including huge improvements in blur speeds
  • Better gradient tools
  • Dockable tool windows (nicely done too)

I’m especially excited about the “fill” tool, as it simplifies tracing scanned sketches and logo bitmaps. The tool creates vectors using a flood-fill algorithm, based on the zoom level and configurable limits (fill method, threshold, gap-closing, etc.). In my tests so far, I’ve been able to turn pencil sketches into vectors quickly1, as well as scans of real-world-objects.2

  1. Normally tracing complex sketches takes me hours
  2. I created the fuji logo by scanning + fill-tracing an old, decaying T-Shirt logo

New natural markup langauges

[Comment]

April 9th, 2008 in Micro Blog

I found two new1 natural-language markup tools after noticing that RestructuredText hadn’t been updated for 2 years.

The first is PanDoc, a multi-format natural-format converter. It converts to and from Markdown, Wikitext, ReST, LATeX, HTML, groff, man, RTF, and more. It’s only downside is that it’s written in Haskell, which is only a problem if you need to run it on Windows.2

The second is Texy, a PHP-based tool with a syntax that distills Wikitext, Markdown, and ReST concepts into one very complete format. It even supports formatting hints and citations for most XHML tags, in a format that surprisingly intuitive.

  1. New to me, at least
  2. Not that Haskell isn’t available on Windows, just that it doesn’t appear to be part of Cygwin

YABE … yet another blogging engine

[Comment]

April 3rd, 2008 in Links

Chryp is a GPL3′d, Tumblr-like blogging engine written in Php. It’s used by such sites as Cameron I/O, hinting that it’s at least marginally capable.

PHP framework benchmarks

[Comment]

March 24th, 2008 in Links

A simple benchmark of CodeIgnitor versus CakePhp (and Symfony).1 While the timings describe a trivial amount of code, they show the basic cost of loading the frameworks.

  1. I hate typing out ‘Symfony’. It’s not clever, it’s not funny, it’s just spelled wrong

First impressions of CodeIgniter

[Comment]

January 26th, 2008 in Micro Blog

[CodeIgniter]I’ve been looking at CodeIgniter this week, a PHP MVC framework built by the ExpressionEngine1 folks at EllisLab. And while I usually find frameworks constraining, CodeIgniter avoids most of the common chafing with a simple, grounded approach.

The library runs under any recent version of PHP (4.x, 5.x), installs in a single directory in the web root of your project, and has minimal dependencies. It provides a simple MVC framework, a large set of helper classes, and a good amount of documentation. The site has two video tutorials, which are enough to get you started.

The pitch

You want a framework that does not require you to use the command line.

You can safely ignore the marketing fluff. The ‘no-command-line’ claim turned me off at first, as there’s nothing wrong with a framework that relies on a command line (and avoiding it misses out on some powerful tools). After working with the kit for a few weeks, it’s obvious that they’re talking about how it’s laid out in a simple, obvious way, and how it doesn’t require running helper scripts for scaffolding and initial setup. It’s not anti-command line at all, rather it’s just not as intensive as Rails (for example).2

The good

CodeIgniter does MVC using a set of simple base classes, routing pages requests in an obvious way through controller class members, who in turn expose views. The kit also provides a modest library of utility classes that fit the MVC abstraction, a simple ActiveRecord DB class, and a set of helper modules that provide common web site functions. I found the approach immediately obvious, and it worked well in all of my test code.

An example:

class Bugs extends Controller {
    function index() {}
    function comments() {}
}

The comments() member is called on the /bugs/comments/ URI, which then can set up queries and calls to display the appropriate view.

The library code is clean and clearly documented. I’m impressed by the layout of the kit, which makes it easy to work with from nearly any tool set.3 The ActiveRecord implementation hasn’t gotten in my way yet, but I haven’t built anything complex enough to properly test it. Performance is better than the larger PHP frameworks,4 as you can easily tailor what is loaded for a given application.

The bad

I’m disappointed that there is no access to tagged Subversion releases of the framework, as it’s a simple way to keep in sync. There also doesn’t seem to be much consideration for AJAX, though nothing about the design prevents it. And, I’m still not sold on abstracting the already-great abstraction of SQL, though many people prefer it.

Here’s an example of an ActiveRecord query:

$this->db->select('title')->from('mytable')->where('id', $id)->limit(10, 20);
$query = $this->db->get();

Luckily CodeIgniter doesn’t require the use of ActiveRecord, though I was surprised to find that the abstraction worked well for the simple problems. The wrapped approach may get in the way with the harder problems, though, and I’m curious to see if mixing ActiveRecord and plain-old SQL is workable.

I couldn’t find many larger sites or projects using the framework, but I have seen many good uses of their ExpressionEngine. The lack of users may harm the kit long term, and it certainly hampers the amount of anecdotal documentation available online. But popularity isn’t the only measure in a tool.

A “good enough” framework

I like CodeIgniter enough to keep using it. It’s clean and simple, which is a feat in itself. I am wary of its lack of popularity, but it’s clear that the company behind it knows what they’re doing. And while it’s not as hip as Rails, it’s clearly capable and mature enough for production.

  1. ExpressionEngine a newer CMS/weblog platform
  2. And we all know that fluff should never be taken internally
  3. I worked mostly in Vim with Nautilus, and Gnome-Terminal
  4. Comparing it to a similar Rails application I wrote in the fall

New features in Php 5.3

[Comment]

November 20th, 2007 in Links

Here’s a great set of articles showing off the new features in Php 5.3: Part 1, 2, 3, 4. It’s fun to watch languages evolve.

Debate formatting, and the question of template mechanisms

[Comment]

November 3rd, 2007 in Links

Which is better, XML or Python for Python templates? I like the Php approach myself (intermixed language/template), as it matches the natural state of each language. The linked debate is interesting, but what I really like is the side-by-side format1.

  1. The “log in to vote” overlay is a bit confusing/noisy, however
Next page [>>]