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

  • Emulating closures in PHP
  • Some ways to use saved state with closures in php
  • Learning lessons from Lisp or patterns and languages in PHP
  • Relations and their domain structures
  • a sample testsuite for the (any/type) base types
  • Overcomplicated design pattern implementations
  • Once again relations, or the need to focus on smaller parts of the big picture. A rant.
  • out of the (php) loop
  • Updated design pattern collection
  • Poor man's macro programming in php (revisited)

guild
Home » blogs » vlado's blog

Scoped php functions or more ways to abuse php

Submitted by vlado on Mon, 2006-07-24 10:09.code | coding | design patterns | php | programming | scribbles | work in progress
Here we go again. In this writeup I'll revisit the ideas of closures, state and closures, partial evaluation and generics.

Basics of 'functions as first class language objects'

Yep. PHP can assign functions to variables. In this sense functions are first class objects. Object and class methods are similar, you can, kind of, assign them to variables and later execute them. What does this change? Well, it improves our ability to abstract common patterns and idioms. There are examples of this in the above writeups as well as in the aspects one and quite a few of the rest of php ones on this site. The basic concept is php callback for example: call_user_func('a_callback_function'); call_user_func(array('AClass', 'aCallback')); call_user_func(array($obj, 'aCallback')); $callback = 'a_callback_function'; $callback(); $callback = array($obj, 'aCallback'); $callback(); What can we use this for? The most obvious and straight forward this is a lightweight state pattern implementation - $a_thing() Will behave differently depending on the value of the $a_thing variable, it's state. Another way to look at php callbacks is as proxies for 'real' functions, class or object methods. Oh, let's not forget is_callable, it's a very useful little number.

Objects and functions

The facility of PHP objects to encapsulate a state and us being able to access this state later can be used to turn them into functions. Let's consider: class aClass{ var $state; function get() { return $state; } function next() { $state=random; } function callbacks() { return array( array($this,'get'), array($this, 'next'));} } $obj = new aClass(); list( $random, $next_random ) = $obj->callbacks(); echo $random(); echo $next_random();

This laboured example shows how to lift the callback definitions from within the object to a different scope. What is interesting to note is that $random() and friend live live in a different scope from the object, at least at first glance, but maintain their state in the object scope.

We can use the above observation to create scoped functions using the technique described in state and closures. The real downside of this method is that the code is not readable and we need to keep a lot in our heads.

Combining the php closures with first class functions (callbacks) gives us better readability at the application point, without sacrificing power.

An interesting way to improve the readability of code using lexical closures is to employ some meta-programming techinques.

Functional style, higher order functions, macro(ish) programming

My personal favourite is hinted in the list style in php writeup". Improving on that technique can lead us to be able to define lexically scoped functions without too much overhead, while maintaining readable, albeit in unusual for php style, code. Something like defun( a_name, args(), begin( $do_something, $do_something_else, ...)) ) The downside if this method is the execution speed. If speed is required, one should be able to lift these definitions into proper php, so that none of the meta functions are used.

vlado's blog | add new comment
Home » blogs » vlado's blog

dikini.net

spreading confusion by accident since 1970