Some ways to use saved state with closures in php

In a previous short post Idescribe a way to emulate closures in php. Using that technique execution environment, otherwise known as a call stack can be saved for future use. This can be put to good use. A couple of patterns or programming techniques could be useful in practice.

State pattern

A closure represents a state => implemenation of a state pattern. This is a bit rich. Usually in OO programming the state pattern is implemented by encapsulating different protocols, for denoted states. This is simple to implement by substituting your protocol specification with a different name. Drupal hooks are a good example implementation of this. The following is a simplisting example implementation of a state pattern based on closures. The last argument of the deduced function is the closure itself.

class function_object {
  var $scope;
  function __get($name) {
    return $this->$scope->$name;
  function __call($name,$args) {
    $func =$this->$state."_$name";
    return call_user_func_args($func,$args);
  function __constructor($scope) {
  function __clone() {
    $this->scope = clone($this->scope);
  function state($state) {
  function call() {
    //the function body - do some work

To save the state for future use, for example as part of an undo cycle:

  $saved = clone($closure);

Call/cc or call with current continuation in php

Closure effectively represent the future of computation. This means that we can say what we want to do, by where we are. Since php doesn't implement any call stack optimisation, the call/cc style of programming makes sense in a limited number of cases. To use this style of programming we need to redefine our functions to use the continuation

f(x,c) : c(f,x);
(var, continuation) -> continuation

and in php

//the function closure call method
function call($x, $continuation) {
$this->scope=$continuation; // emulate c(f,x)
do whatever you want here

OK, this is rather crude. In reality this is very limited - it is assumed that the function is defined in a 'global' scope, so we are putting it in some exhisting stack. It will be cleaner if the continuation implements the stack assignment, rather than rely on the function.

Powered by Drupal, an open source content management system