Posts tagged with functions

PHP Closures

October 16th, 2010

PHP 5.3 has added support for true anonymous functions and closures. Unfortunately, there are a few gotchas.

The main one is that no variables are automatically enclosed: you must specify the variables you want to enclose. So, this will cause an exception:

$a = 2;
$f = function($i) {
  return $i + $a;
}
$f(1); // -> PHP Parse error:  syntax error, unexpected T_VARIABLE

While this is the proper format:

$a = 2;
$f = function($i) use ($a) {
  return $i + $a;
}
$f(1); // -> 3

However, you can also cheat with global:

$a = 2;
$f = function($i) {
  global $a;
  return $i + $a;
}
$f(1); // -> 3

Also, classes that are in scope seem to get imported into the closure’s scope. I believe this is because the class isn’t actually copied into the closure but is instead looked up:

class C {
  static $a = 1;
}
$f = function($i) {
  return $i + C::$a;
}

While normally you would never want to abuse the scope, I guess there may be times when you do.

You can even pass enclosed values by reference:

$a = 2;
$f = function($i) use (&$a) {
  $a = $i;
}
$f(1);
$a == 1 // -> true

Interestingly, I also learned that you can’t mutate a function’s input value:

$f = function($i) {
  return $i++;
}
$f(1); // -> 1

This is also the case in Javascript. To be honest, I’m not sure why this is, since the parameter is not an enclosed variable. But there you go. I’m sure someone who knows more about closures than me can explain why this is.

PS I find the original closures RFC somewhat more useful than the main documentation.

My latest Scala-inspired Javascript library: pf.js

February 4th, 2010

What is a function? While many use the words ‘method’ and ‘function’ interchangeably, more mathematically-inclined programmers make a distinction between functions and methods based on between you are guaranteed a return value. Scala is one programming language that makes a distinction between functions and methods (though it’s actually pretty easy to jump between the two). Great, just like plenty of other languages, nothing special here. Javascript functions are funny beasts. On one hand, they seem to be simple subroutines which can be passed arguments and can enclose variables – pretty standard methods in other words. However, regardless of whether you include return statements in a function, something is actually returned, undefined being returned if you hadn’t specified a return value. Hmm, so they’re kind of like mathematical methods except there’s no way to know without looking at a function’s source code whether the author failed to return a value for the input you gave it or actually did return something, the undefined object.

However, Scala also provides a middle ground with PartialFunctions. PartialFunctions are cool because they are only defined for specific inputs and you can check this. In addition, because they are defined for specific inputs, you can combine several PartialFunctions to create a new PartialFunction which is defined for the union of all the original PartialFunctions’ inputs. Wouldn’t it be cool if Javascript did that too? Well, now it can with pf.js. Enjoy.