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

Similar things

  • Some ways to use saved state with closures in php
  • Learning lessons from Lisp or patterns and languages in PHP
  • scheming templates
  • Scoped php functions or more ways to abuse php
  • Relations and their domain structures
  • scheming away
  • still scheming away
  • LISP-like programming in php
  • macros, higher-order functions, datatypes and design patterns
  • macro processor - initial notes

guild
Home » blogs » vlado's blog

Emulating closures in PHP

Submitted by vlado on Tue, 2006-01-24 09:03.articles | code | design patterns | lisp | php | programming | scheme | scribbles | shorts
In programming languages, a closure is an abstraction that combines a function and a special lexical environment bound to that function (scope). The variables in the lexical environment are designed to retain state information between function calls. Unlike garden-variety functions which retain no memory of what happened in previous calls, closures are capable of storing information across function calls.closulres in computer science (wikipedia)

PHP doesn't have closures as an element of the language. What can we do to emulate it?

We need to be able to encapsulate in one entity scope, state and execution. The most obvious candidate will be a PHP object.

With the introduction of the __get(), __set() and __call() magic functions for PHP classes, something the manual calls overloading, PHP opens the gate to transparently enhance a class to add variables and methods to an object's body. We can abuse the __get() to implemet nested scopes. The state is preserved as part of the object body. The default function (call()) is a protocol to add a uniformity of execution between different function objects.

class function_object { var $scope; function __get($name) { return $this->$scope->$name; } function __constructor($scope) { $this->scope=$scope; } function call() { //the function body - do some work } } If all $scope objects are function objects we get a closure emulation in PHP. This opens the doors to such fancy ideas as namespaces, modules et.al. This technique opens the possibilities for functional style programming techniques and idioms as delayed evaluation, continuation, ...

Update: I've added closures to my design patterns collection

vlado's blog | add new comment
ferrets?
Submitted by Anonymous (not verified) on Tue, 2006-01-24 12:10.

i like =)

reply
another implementation
Submitted by vlado on Tue, 2006-01-24 12:28.

php closures@steike.com

reply
cloning or referencing
Submitted by vlado on Tue, 2006-01-24 16:56.

Depending on application different scope types can be implemented - dynamic or lexical scopes. My initial hunch is that it can be done using different assignment strategies - deep copy or refence.

reply
Somebody posted a comment,
Submitted by vlado on Mon, 2006-07-17 08:09.

Somebody posted a comment, which I accidentally deleted, sorry, saying that they stumble upon another closure implementation

reply
Home » blogs » vlado's blog

dikini.net

spreading confusion by accident since 1970