terminal-auspicious

Topics

programming

php drupal scheme scheming macros design patterns da la

design

design css

random thoughts

scribbles

alter ego

other me 'em that link us my space me linked in

Collections

Programmable web
PHP design patterns

Some ways to use saved state with closures in php

Submitted by vlado on Wed, 2006-01-25 10:23.articles | code | design patterns | lisp | php | programming | scheme | scribbles | shorts

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";
    $args[]=$this;
    return call_user_func_args($func,$args);
  }
  function __constructor($scope) {
    $this->scope=$scope;
  }
  function __clone() {
    $this->scope = clone($this->scope);
  }
  function state($state) {
    $this->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.

vlado's blog | add new comment

Reply

Please solve the math problem above and type in the result. e.g. for 1+1, type 2
The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <br /> <br> <div> <a> <em> <strong> <cite> <pre> <code> <ul> <ol> <li> <dl> <dt> <dd> <h3> <img> <blockquote> <q> <strike> <small> <h4> <h5> <h6>
  • Link to content with [[some text]], where "some text" is the title of existing content or the title of a new piece of content to create. You can also link text to a different title by using [[link to this title|show this text]]. Link to outside URLs with [[http://www.example.com|some text]], or even [[http://www.example.com]].
  • Lines and paragraphs break automatically.
More information about formatting options
Home ยป Some ways to use saved state with closures in php

dikini.net

spreading confusion by accident since 1970