[]RSS

About Archives Artwork Comic Contact Philosophy Projects Tags

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.

HOWTO: Hacking Win32 applications

[Comment]

July 2nd, 2008 in Links

A great introduction to reverse engineering Win32 applications, including a tutorial on hacking the built in Mine Sweeper game.

Javascript object member bindings

[Comment]

July 2nd, 2008 in Links

A great article on the often misunderstood topic of Javascript member binding. Javascript is one of those powerful languages that is treated like a Basic-ish Java. It’s not. It’s more like a Perl-ish Lisp.

Python via Javascript

[Comment]

June 10th, 2008 in Links

A Python introduction for Javascript programmers. One of the clearer Python overviews I’ve seen.

Git intro for SVN users

[Comment]

April 12th, 2008 in Links

A very complete introduction to Git, tailored for Subversion users.

A first Django tutorial

[Comment]

November 13th, 2007 in Links

One of the best developers I know pointed me to a Django tutorial. Django is a Python-based Rails equivalent, that’s even less imposing than Rails. It’s a good corollary framework to Rails,1 as the approach is different enough to make you think a bit.

  1. Especially useful if you’re still trying to find a good framework, or thinking about design approaches

A Objective-C tutorial

[Comment]

October 28th, 2007 in Links

A good tutorial on Objective-C 2.0, highlighting improvements over 1.x. The language has changed a lot since the first time I touched it.

Perl problem solving

[Comment]

August 9th, 2007 in Links

Brian’s Guide to Solving Any Perl Problem, a neat guide to solving Perl scripting problems.

Modeling chess in SQL

[Comment]

May 18th, 2007 in Links

A demented, but useful way of expanding your thinking on SQL: SQLChess - A tutorial on thinking in sets.

Prototype replacement?

[Comment]

May 2nd, 2007 in Links

I bumped into jQuery today, which looks like it’s gaining on the Prototype library. It seems to do batch operations in a more intuitive way, allowing you to toggle one or more selectors in one go.

Next page [>>]