Index: configure.in
===================================================================
RCS file: /repository/php-src/configure.in,v
retrieving revision 1.579.2.52.2.77.2.20
diff -u -p -d -r1.579.2.52.2.77.2.20 configure.in
--- configure.in	2 Jun 2008 16:58:19 -0000	1.579.2.52.2.77.2.20
+++ configure.in	30 Jun 2008 19:31:50 -0000
@@ -1403,7 +1403,8 @@ PHP_ADD_SOURCES(Zend, \
     zend_variables.c zend.c zend_API.c zend_extensions.c zend_hash.c \
     zend_list.c zend_indent.c zend_builtin_functions.c zend_sprintf.c \
     zend_ini.c zend_qsort.c zend_multibyte.c zend_ts_hash.c zend_stream.c \
-    zend_iterators.c zend_interfaces.c zend_exceptions.c zend_strtod.c zend_gc.c)
+    zend_iterators.c zend_interfaces.c zend_exceptions.c zend_strtod.c zend_gc.c \
+    zend_closures.c)
 
 if test -r "$abs_srcdir/Zend/zend_objects.c"; then
   PHP_ADD_SOURCES(Zend, zend_objects.c zend_object_handlers.c zend_objects_API.c \
Index: Zend/Makefile.am
===================================================================
RCS file: /repository/ZendEngine2/Makefile.am,v
retrieving revision 1.57.2.1.2.1.2.1
diff -u -p -d -r1.57.2.1.2.1.2.1 Makefile.am
--- Zend/Makefile.am	16 Mar 2008 21:05:33 -0000	1.57.2.1.2.1.2.1
+++ Zend/Makefile.am	30 Jun 2008 19:31:50 -0000
@@ -17,7 +17,7 @@ libZend_la_SOURCES=\
 	zend_objects_API.c zend_ts_hash.c zend_stream.c \
 	zend_default_classes.c \
 	zend_iterators.c zend_interfaces.c zend_exceptions.c \
-	zend_strtod.c
+	zend_strtod.c zend_closures.c
 
 libZend_la_LDFLAGS =
 libZend_la_LIBADD = @ZEND_EXTRA_LIBS@
Index: Zend/Zend.dsp
===================================================================
RCS file: /repository/ZendEngine2/Zend.dsp,v
retrieving revision 1.25.2.1.2.1
diff -u -p -d -r1.25.2.1.2.1 Zend.dsp
--- Zend/Zend.dsp	5 Dec 2006 08:07:57 -0000	1.25.2.1.2.1
+++ Zend/Zend.dsp	30 Jun 2008 19:31:50 -0000
@@ -123,6 +123,10 @@ SOURCE=.\zend_builtin_functions.c
 # End Source File
 # Begin Source File
 
+SOURCE=.\zend_closures.c
+# End Source File
+# Begin Source File
+
 SOURCE=.\zend_compile.c
 # End Source File
 # Begin Source File
Index: Zend/ZendTS.dsp
===================================================================
RCS file: /repository/ZendEngine2/ZendTS.dsp,v
retrieving revision 1.47.2.1.2.2
diff -u -p -d -r1.47.2.1.2.2 ZendTS.dsp
--- Zend/ZendTS.dsp	5 Dec 2006 08:07:57 -0000	1.47.2.1.2.2
+++ Zend/ZendTS.dsp	30 Jun 2008 19:31:50 -0000
@@ -148,6 +148,10 @@ SOURCE=.\zend_builtin_functions.c
 # End Source File
 # Begin Source File
 
+SOURCE=.\zend_closures.c
+# End Source File
+# Begin Source File
+
 SOURCE=.\zend_compile.c
 # End Source File
 # Begin Source File
Index: Zend/zend.h
===================================================================
RCS file: /repository/ZendEngine2/zend.h,v
retrieving revision 1.293.2.11.2.9.2.23
diff -u -p -d -r1.293.2.11.2.9.2.23 zend.h
--- Zend/zend.h	27 Jun 2008 18:44:39 -0000	1.293.2.11.2.9.2.23
+++ Zend/zend.h	30 Jun 2008 19:31:51 -0000
@@ -518,6 +518,8 @@ typedef int (*zend_write_func_t)(const c
 #define IS_CONSTANT_TYPE_MASK	0x0f
 #define IS_CONSTANT_RT_NS_CHECK	0x10
 #define IS_CONSTANT_INDEX		0x80
+#define IS_LEXICAL_VAR			0x20
+#define IS_LEXICAL_REF			0x40
 
 /* overloaded elements data types */
 #define OE_IS_ARRAY		(1<<0)
Index: Zend/zend_API.c
===================================================================
RCS file: /repository/ZendEngine2/zend_API.c,v
retrieving revision 1.296.2.27.2.34.2.38
diff -u -p -d -r1.296.2.27.2.34.2.38 zend_API.c
--- Zend/zend_API.c	5 Jun 2008 18:53:06 -0000	1.296.2.27.2.34.2.38
+++ Zend/zend_API.c	30 Jun 2008 19:31:51 -0000
@@ -26,6 +26,7 @@
 #include "zend_modules.h"
 #include "zend_constants.h"
 #include "zend_exceptions.h"
+#include "zend_closures.h"
 
 #ifdef HAVE_STDARG_H
 #include <stdarg.h>
@@ -2615,6 +2616,16 @@ ZEND_API zend_bool zend_is_callable_ex(z
 			}
 			return 0;
 
+		case IS_OBJECT:
+			if (zend_get_closure(callable, ce_ptr, fptr_ptr, NULL, zobj_ptr_ptr TSRMLS_CC) == SUCCESS) {
+				if (callable_name) {
+					*callable_name_len = strlen((*fptr_ptr)->common.function_name);
+					*callable_name = estrndup((*fptr_ptr)->common.function_name, *callable_name_len);
+				}									
+				return 1;
+			}
+			/* break missing intentionally */
+
 		default:
 			if (callable_name) {
 				zval expr_copy;
Index: Zend/zend_closures.c
===================================================================
RCS file: Zend/zend_closures.c
diff -N Zend/zend_closures.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/zend_closures.c	30 Jun 2008 19:31:51 -0000
@@ -0,0 +1,295 @@
+/*
+   +----------------------------------------------------------------------+
+   | Zend Engine                                                          |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1998-2008 Zend Technologies Ltd. (http://www.zend.com) |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 2.00 of the Zend license,     |
+   | that is bundled with this package in the file LICENSE, and is        | 
+   | available through the world-wide-web at the following url:           |
+   | http://www.zend.com/license/2_00.txt.                                |
+   | If you did not receive a copy of the Zend license and are unable to  |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@zend.com so we can mail you a copy immediately.              |
+   +----------------------------------------------------------------------+
+   | Authors: Christian Seiler <chris_se@gmx.net>                         |
+   |          Dmitry Stogov <dmitry@zend.com>                             |
+   +----------------------------------------------------------------------+
+*/
+
+/* $Id: zend_interfaces.c,v 1.33.2.4.2.6.2.4 2008/01/24 18:07:45 dmitry Exp $ */
+
+#include "zend.h"
+#include "zend_API.h"
+#include "zend_closures.h"
+#include "zend_objects.h"
+#include "zend_objects_API.h"
+#include "zend_globals.h"
+
+typedef struct _zend_closure {
+	zend_object    std;
+	zend_function  func;
+	zval          *this_ptr;
+} zend_closure;
+
+static zend_class_entry *zend_ce_closure;
+static zend_object_handlers closure_handlers;
+
+ZEND_METHOD(Closure, __invoke) /* {{{ */
+{
+	zval ***arguments;
+	zval *closure_result_ptr = NULL;
+
+	arguments = emalloc(sizeof(zval**) * ZEND_NUM_ARGS());
+	if (zend_get_parameters_array_ex(ZEND_NUM_ARGS(), arguments) == FAILURE) {
+		efree(arguments);
+		zend_error(E_ERROR, "Cannot get arguments for calling closure");
+		RETURN_FALSE;
+	}
+
+	if (!return_value_ptr) {
+		return_value_ptr = &closure_result_ptr;
+	}
+
+	if (call_user_function_ex(CG(function_table), NULL, this_ptr, return_value_ptr, ZEND_NUM_ARGS(), arguments, 1, NULL TSRMLS_CC) == FAILURE) {
+		efree(arguments);
+		RETURN_FALSE;
+	}
+
+	efree(arguments);
+	if (closure_result_ptr) {
+		RETVAL_ZVAL(closure_result_ptr, 1, 1);
+	}
+}
+/* }}} */
+
+const static zend_function_entry closure_functions[] = { /* {{{ */
+	ZEND_ME(Closure, __invoke, NULL, 0)
+	{NULL, NULL, NULL}
+};
+/* }}} */
+
+static zend_function *zend_closure_get_constructor(zval *object TSRMLS_DC) /* {{{ */
+{
+	zend_error(E_ERROR, "Instantiation of 'Closure' is not allowed");
+	return NULL;
+}
+/* }}} */
+
+static int zend_closure_compare_objects(zval *o1, zval *o2 TSRMLS_DC) /* {{{ */
+{
+	return (Z_OBJ_HANDLE_P(o1) != Z_OBJ_HANDLE_P(o2));
+}
+/* }}} */
+
+static int zend_closure_cast_object_tostring(zval *readobj, zval *writeobj, int type TSRMLS_DC) /* {{{ */
+{
+	zend_class_entry *ce;
+
+	switch (type) {
+		case IS_STRING:
+			INIT_PZVAL(writeobj);
+			ZVAL_STRINGL(writeobj, "Closure object", sizeof("Closure object"), 1);
+			return SUCCESS;
+		default:
+			ce = Z_OBJCE_P(readobj);
+			zend_error(E_NOTICE, "Object of class %s could not be converted to %s", ce->name, zend_get_type_by_const(type));
+			INIT_PZVAL(writeobj);
+			Z_TYPE_P(writeobj) = IS_NULL;
+			break;
+	}
+	return FAILURE;
+}
+/* }}} */
+
+static void zend_closure_free_storage(void *object TSRMLS_DC) /* {{{ */
+{
+	zend_closure *closure = (zend_closure *)object;
+
+	zend_object_std_dtor(&closure->std TSRMLS_CC);
+
+	if (closure->func.type == ZEND_USER_FUNCTION) {
+		zend_execute_data *ex = EG(current_execute_data);
+		while (ex) {
+			if (ex->op_array == &closure->func.op_array) {
+				zend_error(E_ERROR, "Cannot destroy active lambda function");
+			}
+			ex = ex->prev_execute_data;
+		}
+		destroy_op_array(&closure->func.op_array TSRMLS_CC);
+	}
+
+	if (closure->this_ptr) {
+		zval_ptr_dtor(&closure->this_ptr);
+	}
+
+	efree(closure);
+}
+/* }}} */
+
+static zend_object_value zend_closure_new(zend_class_entry *class_type TSRMLS_DC) /* {{{ */
+{
+	zend_closure *closure;
+	zend_object_value object;
+
+	closure = emalloc(sizeof(zend_closure));
+	memset(closure, 0, sizeof(zend_closure));
+
+	zend_object_std_init(&closure->std, class_type TSRMLS_CC);
+	
+	object.handle = zend_objects_store_put(closure, (zend_objects_store_dtor_t)zend_objects_destroy_object, (zend_objects_free_object_storage_t) zend_closure_free_storage, NULL TSRMLS_CC);
+	object.handlers = &closure_handlers;
+	
+	return object;
+}
+/* }}} */
+
+void zend_register_closure_ce(TSRMLS_D) /* {{{ */
+{
+	zend_class_entry ce;
+
+	INIT_CLASS_ENTRY(ce, "Closure", closure_functions);
+	zend_ce_closure = zend_register_internal_class(&ce TSRMLS_CC);
+	zend_ce_closure->ce_flags |= ZEND_ACC_FINAL_CLASS;
+	zend_ce_closure->create_object = zend_closure_new;
+
+	memcpy(&closure_handlers, zend_get_std_object_handlers(), sizeof(zend_object_handlers));
+	closure_handlers.get_constructor = zend_closure_get_constructor;
+	closure_handlers.compare_objects = zend_closure_compare_objects;
+	closure_handlers.cast_object = zend_closure_cast_object_tostring;
+	closure_handlers.clone_obj = NULL;
+}
+/* }}} */
+
+static int zval_copy_static_var(zval **p, int num_args, va_list args, zend_hash_key *key) /* {{{ */
+{
+	HashTable *target = va_arg(args, HashTable*);
+	zend_bool is_ref;
+
+	if (Z_TYPE_PP(p) & (IS_LEXICAL_VAR|IS_LEXICAL_REF)) {
+		TSRMLS_FETCH();
+		is_ref = Z_TYPE_PP(p) & IS_LEXICAL_REF;
+
+		if (!EG(active_symbol_table)) {
+			zend_rebuild_symbol_table(TSRMLS_C);
+		}
+		if (zend_hash_quick_find(EG(active_symbol_table), key->arKey, key->nKeyLength, key->h, (void **) &p) == FAILURE) {
+			if (is_ref) {
+				zval *tmp;
+
+				ALLOC_INIT_ZVAL(tmp);
+				Z_SET_ISREF_P(tmp);
+				zend_hash_quick_add(EG(active_symbol_table), key->arKey, key->nKeyLength, key->h, &tmp, sizeof(zval*), (void**)&p);
+			} else {
+				p = &EG(uninitialized_zval_ptr);
+				zend_error(E_NOTICE,"Undefined variable: %s", key->arKey);
+			}
+		} else {
+			if (is_ref) {
+				SEPARATE_ZVAL_TO_MAKE_IS_REF(p);
+			} else if (Z_ISREF_PP(p)) {
+				SEPARATE_ZVAL(p);
+			}
+		}
+	}
+	if (zend_hash_quick_add(target, key->arKey, key->nKeyLength, key->h, p, sizeof(zval*), NULL) == SUCCESS) {
+		Z_ADDREF_PP(p);
+	}
+	return ZEND_HASH_APPLY_KEEP;
+}
+/* }}} */
+
+ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zval *this_ptr TSRMLS_DC) /* {{{ */
+{
+	zend_closure *closure;
+
+	object_init_ex(res, zend_ce_closure);
+
+	closure = (zend_closure *)zend_object_store_get_object(res);
+
+	closure->func = *func;
+
+	if (closure->func.type == ZEND_USER_FUNCTION) {
+		if (closure->func.op_array.static_variables) {
+			HashTable *static_variables = closure->func.op_array.static_variables;
+	
+			ALLOC_HASHTABLE(closure->func.op_array.static_variables);
+			zend_hash_init(closure->func.op_array.static_variables, zend_hash_num_elements(static_variables), NULL, ZVAL_PTR_DTOR, 0);
+			zend_hash_apply_with_arguments(static_variables, (apply_func_args_t)zval_copy_static_var, 1, closure->func.op_array.static_variables);
+		}
+		(*closure->func.op_array.refcount)++;
+	}
+
+	closure->func.common.scope = scope;
+	if (scope) {
+		closure->func.common.fn_flags |= ZEND_ACC_PUBLIC;
+		if (this_ptr && (closure->func.common.fn_flags & ZEND_ACC_STATIC) == 0) {
+			closure->this_ptr = this_ptr;
+			Z_ADDREF_P(this_ptr);
+		} else {
+			closure->this_ptr = NULL;
+		}
+	} else {
+		closure->this_ptr = NULL;
+	}
+}
+/* }}} */
+
+ZEND_API int zend_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr, zval ***zobj_ptr_ptr TSRMLS_DC) /* {{{ */
+{
+	if (Z_TYPE_P(obj) == IS_OBJECT) {
+		zend_class_entry *ce = Z_OBJCE_P(obj);
+
+		if (ce == zend_ce_closure) {
+			zend_closure *closure = (zend_closure *)zend_object_store_get_object(obj);
+
+			*fptr_ptr = &closure->func;
+			if (closure->this_ptr) {
+				if (zobj_ptr) {
+					*zobj_ptr = closure->this_ptr;
+				}
+				if (zobj_ptr_ptr) {
+					*zobj_ptr_ptr = &closure->this_ptr;
+				}
+				*ce_ptr = Z_OBJCE_P(closure->this_ptr);
+			} else {
+				if (zobj_ptr) {
+					*zobj_ptr = NULL;
+				}
+				if (zobj_ptr_ptr) {
+					*zobj_ptr_ptr = NULL;
+				}
+				*ce_ptr = closure->func.common.scope;
+			}
+			return SUCCESS;
+		} else if (zend_hash_find(&ce->function_table, "__invoke", sizeof("__invoke"), (void**)fptr_ptr) == SUCCESS) {
+			*ce_ptr = ce;
+			if ((*fptr_ptr)->common.fn_flags & ZEND_ACC_STATIC) {
+				if (zobj_ptr) {
+					*zobj_ptr = NULL;
+				}
+				if (zobj_ptr_ptr) {
+					*zobj_ptr_ptr = NULL;
+				}
+			} else {
+				if (zobj_ptr) {
+					*zobj_ptr = obj;
+				}
+				if (zobj_ptr_ptr) {
+					*zobj_ptr_ptr = NULL;
+				}
+			}
+			return SUCCESS;
+		}
+	}
+	return FAILURE;
+}
+/* }}} */
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ */
Index: Zend/zend_closures.h
===================================================================
RCS file: Zend/zend_closures.h
diff -N Zend/zend_closures.h
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/zend_closures.h	30 Jun 2008 19:31:51 -0000
@@ -0,0 +1,42 @@
+/*
+   +----------------------------------------------------------------------+
+   | Zend Engine                                                          |
+   +----------------------------------------------------------------------+
+   | Copyright (c) 1998-2008 Zend Technologies Ltd. (http://www.zend.com) |
+   +----------------------------------------------------------------------+
+   | This source file is subject to version 2.00 of the Zend license,     |
+   | that is bundled with this package in the file LICENSE, and is        | 
+   | available through the world-wide-web at the following url:           |
+   | http://www.zend.com/license/2_00.txt.                                |
+   | If you did not receive a copy of the Zend license and are unable to  |
+   | obtain it through the world-wide-web, please send a note to          |
+   | license@zend.com so we can mail you a copy immediately.              |
+   +----------------------------------------------------------------------+
+   | Authors: Christian Seiler <chris_se@gmx.net>                         |
+   |          Dmitry Stogov <dmitry@zend.com>                             |
+   +----------------------------------------------------------------------+
+*/
+
+/* $Id$ */
+
+#ifndef ZEND_CLOSURES_H
+#define ZEND_CLOSURES_H
+
+BEGIN_EXTERN_C()
+
+void zend_register_closure_ce(TSRMLS_D);
+
+ZEND_API void zend_create_closure(zval *res, zend_function *op_array, zend_class_entry *scope, zval *this_ptr TSRMLS_DC);
+ZEND_API int zend_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function **fptr_ptr, zval **zobj_ptr, zval ***zobj_ptr_ptr TSRMLS_DC);
+
+END_EXTERN_C()
+
+#endif
+
+/*
+ * Local variables:
+ * tab-width: 4
+ * c-basic-offset: 4
+ * indent-tabs-mode: t
+ * End:
+ */
Index: Zend/zend_compile.c
===================================================================
RCS file: /repository/ZendEngine2/zend_compile.c,v
retrieving revision 1.647.2.27.2.41.2.70
diff -u -p -d -r1.647.2.27.2.41.2.70 zend_compile.c
--- Zend/zend_compile.c	29 Jun 2008 08:21:34 -0000	1.647.2.27.2.41.2.70
+++ Zend/zend_compile.c	30 Jun 2008 19:31:51 -0000
@@ -1401,6 +1401,33 @@ void zend_do_begin_function_declaration(
 	CG(labels) = NULL;
 }
 
+void zend_do_begin_lambda_function_declaration(znode *result, znode *function_token, int return_reference, int is_static TSRMLS_DC)
+{
+	znode          function_name;
+	zend_op_array *current_op_array = CG(active_op_array);
+	int            current_op_number = get_next_op_number(CG(active_op_array));
+	zend_op       *current_op;
+
+	function_name.op_type = IS_CONST;
+	ZVAL_STRINGL(&function_name.u.constant, "lambda", sizeof("lambda")-1, 1);
+
+	zend_do_begin_function_declaration(function_token, &function_name, 0, return_reference, NULL TSRMLS_CC);
+
+	result->op_type = IS_TMP_VAR;
+	result->u.var = get_temporary_variable(current_op_array);;
+
+	current_op = &current_op_array->opcodes[current_op_number];
+	current_op->opcode = ZEND_DECLARE_LAMBDA_FUNCTION;
+	zval_dtor(&current_op->op2.u.constant);
+	ZVAL_LONG(&current_op->op2.u.constant, zend_hash_func(Z_STRVAL(current_op->op1.u.constant), Z_STRLEN(current_op->op1.u.constant)));
+	current_op->result = *result;
+	if (is_static) {
+	    CG(active_op_array)->fn_flags |= ZEND_ACC_STATIC;
+	}
+    CG(active_op_array)->fn_flags |= ZEND_ACC_CLOSURE;
+}
+
+
 void zend_do_handle_exception(TSRMLS_D)
 {
 	zend_op *opline = get_next_op(CG(active_op_array) TSRMLS_CC);
@@ -4155,13 +4182,13 @@ void zend_do_fetch_static_variable(znode
 	}
 
 	opline = get_next_op(CG(active_op_array) TSRMLS_CC);
-	opline->opcode = ZEND_FETCH_W;		/* the default mode must be Write, since fetch_simple_variable() is used to define function arguments */
+	opline->opcode = (fetch_type == ZEND_FETCH_LEXICAL) ? ZEND_FETCH_R : ZEND_FETCH_W;		/* the default mode must be Write, since fetch_simple_variable() is used to define function arguments */
 	opline->result.op_type = IS_VAR;
 	opline->result.u.EA.type = 0;
 	opline->result.u.var = get_temporary_variable(CG(active_op_array));
 	opline->op1 = *varname;
 	SET_UNUSED(opline->op2);
-	opline->op2.u.EA.type = fetch_type;
+	opline->op2.u.EA.type = ZEND_FETCH_STATIC;
 	result = opline->result;
 
 	if (varname->op_type == IS_CONST) {
@@ -4169,12 +4196,39 @@ void zend_do_fetch_static_variable(znode
 	}
 	fetch_simple_variable(&lval, varname, 0 TSRMLS_CC); /* Relies on the fact that the default fetch is BP_VAR_W */
 
-	zend_do_assign_ref(NULL, &lval, &result TSRMLS_CC);
+	if (fetch_type == ZEND_FETCH_LEXICAL) {
+		znode dummy;
+
+		zend_do_begin_variable_parse(TSRMLS_C);
+		zend_do_assign(&dummy, &lval, &result TSRMLS_CC);
+		zend_do_free(&dummy);
+	} else {
+		zend_do_assign_ref(NULL, &lval, &result TSRMLS_CC);
+	}
 	CG(active_op_array)->opcodes[CG(active_op_array)->last-1].result.u.EA.type |= EXT_TYPE_UNUSED;
 
 /*	zval_dtor(&varname->u.constant); */
 }
 
+void zend_do_fetch_lexical_variable(znode *varname, zend_bool is_ref TSRMLS_DC)
+{
+	znode value;
+
+	if (Z_STRLEN(varname->u.constant) == sizeof("this") - 1 &&
+	    memcmp(Z_STRVAL(varname->u.constant), "this", sizeof("this") - 1) == 0) {
+		zend_error(E_COMPILE_ERROR, "Cannot use $this as lexical variable");
+		return;
+	}
+
+	value.op_type = IS_CONST;
+	ZVAL_NULL(&value.u.constant);
+	Z_TYPE(value.u.constant) |= is_ref ? IS_LEXICAL_REF : IS_LEXICAL_VAR;
+	Z_SET_REFCOUNT_P(&value.u.constant, 1);
+	Z_UNSET_ISREF_P(&value.u.constant);
+	
+	zend_do_fetch_static_variable(varname, &value, is_ref ? ZEND_FETCH_STATIC : ZEND_FETCH_LEXICAL TSRMLS_CC);
+}
+
 void zend_do_fetch_global_variable(znode *varname, znode *static_assignment, int fetch_type TSRMLS_DC)
 {
 	zend_op *opline;
Index: Zend/zend_compile.h
===================================================================
RCS file: /repository/ZendEngine2/zend_compile.h,v
retrieving revision 1.316.2.8.2.12.2.25
diff -u -p -d -r1.316.2.8.2.12.2.25 zend_compile.h
--- Zend/zend_compile.h	11 Jun 2008 13:18:39 -0000	1.316.2.8.2.12.2.25
+++ Zend/zend_compile.h	30 Jun 2008 19:31:51 -0000
@@ -151,6 +151,8 @@ typedef struct _zend_try_catch_element {
 /* class implement interface(s) flag */
 #define ZEND_ACC_IMPLEMENT_INTERFACES 0x80000
 
+#define ZEND_ACC_CLOSURE              0x100000
+
 
 char *zend_visibility_string(zend_uint fn_flags);
 
@@ -429,6 +431,9 @@ void zend_do_end_function_call(znode *fu
 void zend_do_return(znode *expr, int do_end_vparse TSRMLS_DC);
 void zend_do_handle_exception(TSRMLS_D);
 
+void zend_do_begin_lambda_function_declaration(znode *result, znode *function_token, int return_reference, int is_static TSRMLS_DC);
+void zend_do_fetch_lexical_variable(znode *varname, zend_bool is_ref TSRMLS_DC);
+
 void zend_do_try(znode *try_token TSRMLS_DC);
 void zend_do_begin_catch(znode *try_token, znode *catch_class, znode *catch_var, znode *first_catch TSRMLS_DC);
 void zend_do_end_catch(znode *try_token TSRMLS_DC);
@@ -614,7 +619,8 @@ int zendlex(znode *zendlval TSRMLS_DC);
 #define ZEND_FETCH_LOCAL			1
 #define ZEND_FETCH_STATIC			2
 #define ZEND_FETCH_STATIC_MEMBER	3
-#define ZEND_FETCH_GLOBAL_LOCK	4
+#define ZEND_FETCH_GLOBAL_LOCK		4
+#define ZEND_FETCH_LEXICAL			5
 
 
 /* class fetches */
Index: Zend/zend_default_classes.c
===================================================================
RCS file: /repository/ZendEngine2/zend_default_classes.c,v
retrieving revision 1.59.2.2.2.1.2.1
diff -u -p -d -r1.59.2.2.2.1.2.1 zend_default_classes.c
--- Zend/zend_default_classes.c	31 Dec 2007 07:17:04 -0000	1.59.2.2.2.1.2.1
+++ Zend/zend_default_classes.c	30 Jun 2008 19:31:51 -0000
@@ -24,6 +24,7 @@
 #include "zend_builtin_functions.h"
 #include "zend_interfaces.h"
 #include "zend_exceptions.h"
+#include "zend_closures.h"
 
 
 ZEND_API void zend_register_default_classes(TSRMLS_D)
@@ -31,6 +32,7 @@ ZEND_API void zend_register_default_clas
 	zend_register_interfaces(TSRMLS_C);
 	zend_register_default_exception(TSRMLS_C);
 	zend_register_iterator_wrapper(TSRMLS_C);
+	zend_register_closure_ce(TSRMLS_C);
 }
 
 /*
Index: Zend/zend_execute.c
===================================================================
RCS file: /repository/ZendEngine2/zend_execute.c,v
retrieving revision 1.716.2.12.2.24.2.30
diff -u -p -d -r1.716.2.12.2.24.2.30 zend_execute.c
--- Zend/zend_execute.c	11 Jun 2008 13:18:39 -0000	1.716.2.12.2.24.2.30
+++ Zend/zend_execute.c	30 Jun 2008 19:31:51 -0000
@@ -34,6 +34,7 @@
 #include "zend_ini.h"
 #include "zend_exceptions.h"
 #include "zend_interfaces.h"
+#include "zend_closures.h"
 #include "zend_vm.h"
 
 /* Virtual current working directory support */
Index: Zend/zend_execute_API.c
===================================================================
RCS file: /repository/ZendEngine2/zend_execute_API.c,v
retrieving revision 1.331.2.20.2.24.2.42
diff -u -p -d -r1.331.2.20.2.24.2.42 zend_execute_API.c
--- Zend/zend_execute_API.c	5 Jun 2008 18:50:29 -0000	1.331.2.20.2.24.2.42
+++ Zend/zend_execute_API.c	30 Jun 2008 19:31:51 -0000
@@ -30,6 +30,7 @@
 #include "zend_constants.h"
 #include "zend_extensions.h"
 #include "zend_exceptions.h"
+#include "zend_closures.h"
 #include "zend_vm.h"
 #ifdef HAVE_SYS_TIME_H
 #include <sys/time.h>
@@ -826,7 +827,11 @@ int zend_call_function(zend_fcall_info *
 			}
 		}
 
-		if (Z_TYPE_P(fci->function_name) != IS_STRING) {
+		if (Z_TYPE_P(fci->function_name) == IS_OBJECT) {
+			if (zend_get_closure(fci->function_name, &calling_scope, &EX(function_state).function, NULL, &fci->object_pp TSRMLS_CC) == SUCCESS) {
+				goto init_fci_cache;
+			}
+		} else if (Z_TYPE_P(fci->function_name) != IS_STRING) {
 			return FAILURE;
 		}
 
@@ -935,6 +940,8 @@ int zend_call_function(zend_fcall_info *
 				return FAILURE;
 			}
 		}
+
+init_fci_cache:
 		if (fci_cache &&
 			(EX(function_state).function->type != ZEND_INTERNAL_FUNCTION ||
 			((zend_internal_function*)EX(function_state).function)->handler != zend_std_call_user_call)
Index: Zend/zend_language_parser.y
===================================================================
RCS file: /repository/ZendEngine2/zend_language_parser.y,v
retrieving revision 1.160.2.4.2.8.2.21
diff -u -p -d -r1.160.2.4.2.8.2.21 zend_language_parser.y
--- Zend/zend_language_parser.y	8 Jun 2008 09:38:47 -0000	1.160.2.4.2.8.2.21
+++ Zend/zend_language_parser.y	30 Jun 2008 19:31:51 -0000
@@ -302,7 +302,7 @@ is_reference:
 
 
 unticked_function_declaration_statement:
-		T_FUNCTION { $1.u.opline_num = CG(zend_lineno); } is_reference T_STRING { zend_do_begin_function_declaration(&$1, &$4, 0, $3.op_type, NULL TSRMLS_CC); }
+		function is_reference T_STRING { zend_do_begin_function_declaration(&$1, &$3, 0, $2.op_type, NULL TSRMLS_CC); }
 			'(' parameter_list ')' '{' inner_statement_list '}' { zend_do_end_function_declaration(&$1 TSRMLS_CC); }
 ;
 
@@ -510,8 +510,8 @@ class_statement_list:
 class_statement:
 		variable_modifiers { CG(access_type) = Z_LVAL($1.u.constant); } class_variable_declaration ';'
 	|	class_constant_declaration ';'
-	|	method_modifiers T_FUNCTION { $2.u.opline_num = CG(zend_lineno); } is_reference T_STRING { zend_do_begin_function_declaration(&$2, &$5, 1, $4.op_type, &$1 TSRMLS_CC); } '('
-			parameter_list ')' method_body { zend_do_abstract_method(&$5, &$1, &$10 TSRMLS_CC); zend_do_end_function_declaration(&$2 TSRMLS_CC); }
+	|	method_modifiers function is_reference T_STRING { zend_do_begin_function_declaration(&$2, &$4, 1, $3.op_type, &$1 TSRMLS_CC); } '('
+			parameter_list ')' method_body { zend_do_abstract_method(&$4, &$1, &$9 TSRMLS_CC); zend_do_end_function_declaration(&$2 TSRMLS_CC); }
 ;
 
 
@@ -643,10 +643,30 @@ expr_without_variable:
 	|	T_ARRAY '(' array_pair_list ')' { $$ = $3; }
 	|	'`' encaps_list '`' { zend_do_shell_exec(&$$, &$2 TSRMLS_CC); }
 	|	T_PRINT expr  { zend_do_print(&$$, &$2 TSRMLS_CC); }
+	|	function is_reference '(' { zend_do_begin_lambda_function_declaration(&$$, &$1, $2.op_type, 0 TSRMLS_CC); }
+			parameter_list ')' lexical_vars '{' inner_statement_list '}' {  zend_do_end_function_declaration(&$1 TSRMLS_CC); $$ = $4; }
+	|	T_STATIC function is_reference '(' { zend_do_begin_lambda_function_declaration(&$$, &$2, $3.op_type, 1 TSRMLS_CC); }
+			parameter_list ')' lexical_vars '{' inner_statement_list '}' {  zend_do_end_function_declaration(&$2 TSRMLS_CC); $$ = $5; }
+;
+
+function:
+	T_FUNCTION { $$.u.opline_num = CG(zend_lineno); }
+;
+
+lexical_vars:
+		/* emptry */
+	|	T_USE '(' lexical_var_list ')'
+;
+
+lexical_var_list:
+		lexical_var_list ',' T_VARIABLE			{ zend_do_fetch_lexical_variable(&$3, 0 TSRMLS_CC); }
+	|	lexical_var_list ',' '&' T_VARIABLE		{ zend_do_fetch_lexical_variable(&$4, 1 TSRMLS_CC); }
+	|	T_VARIABLE								{ zend_do_fetch_lexical_variable(&$1, 0 TSRMLS_CC); }
+	|	'&' T_VARIABLE							{ zend_do_fetch_lexical_variable(&$2, 1 TSRMLS_CC); }
 ;
 
 function_call:
-		T_STRING	'(' { $2.u.opline_num = zend_do_begin_function_call(&$1, 1 TSRMLS_CC); }
+		T_STRING '(' { $2.u.opline_num = zend_do_begin_function_call(&$1, 1 TSRMLS_CC); }
 				function_call_parameter_list
 				')' { zend_do_end_function_call(&$1, &$$, &$4, 0, $2.u.opline_num TSRMLS_CC); zend_do_extended_fcall_end(TSRMLS_C); }
 	|	T_PAAMAYIM_NEKUDOTAYIM T_STRING '(' { $3.u.opline_num = zend_do_begin_function_call(&$2, 0 TSRMLS_CC); }
Index: Zend/zend_vm_def.h
===================================================================
RCS file: /repository/ZendEngine2/zend_vm_def.h,v
retrieving revision 1.59.2.29.2.48.2.58
diff -u -p -d -r1.59.2.29.2.48.2.58 zend_vm_def.h
--- Zend/zend_vm_def.h	11 Jun 2008 13:18:39 -0000	1.59.2.29.2.48.2.58
+++ Zend/zend_vm_def.h	30 Jun 2008 19:31:52 -0000
@@ -2029,6 +2029,15 @@ ZEND_VM_HANDLER(59, ZEND_INIT_FCALL_BY_N
 	} else {
 		function_name = GET_OP2_ZVAL_PTR(BP_VAR_R);
 
+		if (Z_TYPE_P(function_name) == IS_OBJECT &&
+			zend_get_closure(function_name, &EX(called_scope), &EX(fbc), &EX(object), NULL TSRMLS_CC) == SUCCESS) {
+			if (EX(object)) {
+				Z_ADDREF_P(EX(object));
+			}
+			FREE_OP2();
+			ZEND_VM_NEXT_OPCODE();
+		}
+
 		if (Z_TYPE_P(function_name) != IS_STRING) {
 			zend_error_noreturn(E_ERROR, "Function name must be a string");
 		}
@@ -4327,4 +4336,19 @@ ZEND_VM_HANDLER(143, ZEND_DECLARE_CONST,
 	ZEND_VM_NEXT_OPCODE();
 }
 
+ZEND_VM_HANDLER(153, ZEND_DECLARE_LAMBDA_FUNCTION, CONST, CONST)
+{
+	zend_op *opline = EX(opline);
+	zend_op_array *op_array;
+
+	if (zend_hash_quick_find(EG(function_table), Z_STRVAL(opline->op1.u.constant), Z_STRLEN(opline->op1.u.constant), Z_LVAL(opline->op2.u.constant), (void *) &op_array) == FAILURE ||
+	    op_array->type != ZEND_USER_FUNCTION) {
+		zend_error_noreturn(E_ERROR, "Base lambda function for closure not found");
+	}
+
+	zend_create_closure(&EX_T(opline->result.u.var).tmp_var, op_array, EG(scope), EG(This) TSRMLS_CC);
+
+	ZEND_VM_NEXT_OPCODE();
+}
+
 ZEND_VM_EXPORT_HELPER(zend_do_fcall, zend_do_fcall_common_helper)
Index: Zend/zend_vm_execute.h
===================================================================
RCS file: /repository/ZendEngine2/zend_vm_execute.h,v
retrieving revision 1.62.2.30.2.49.2.58
diff -u -p -d -r1.62.2.30.2.49.2.58 zend_vm_execute.h
--- Zend/zend_vm_execute.h	11 Jun 2008 13:18:39 -0000	1.62.2.30.2.49.2.58
+++ Zend/zend_vm_execute.h	30 Jun 2008 19:31:52 -0000
@@ -747,6 +747,15 @@ static int ZEND_FASTCALL  ZEND_INIT_FCAL
 	} else {
 		function_name = &opline->op2.u.constant;
 
+		if (Z_TYPE_P(function_name) == IS_OBJECT &&
+			zend_get_closure(function_name, &EX(called_scope), &EX(fbc), &EX(object), NULL TSRMLS_CC) == SUCCESS) {
+			if (EX(object)) {
+				Z_ADDREF_P(EX(object));
+			}
+
+			ZEND_VM_NEXT_OPCODE();
+		}
+
 		if (Z_TYPE_P(function_name) != IS_STRING) {
 			zend_error_noreturn(E_ERROR, "Function name must be a string");
 		}
@@ -935,6 +944,15 @@ static int ZEND_FASTCALL  ZEND_INIT_FCAL
 	} else {
 		function_name = _get_zval_ptr_tmp(&opline->op2, EX(Ts), &free_op2 TSRMLS_CC);
 
+		if (Z_TYPE_P(function_name) == IS_OBJECT &&
+			zend_get_closure(function_name, &EX(called_scope), &EX(fbc), &EX(object), NULL TSRMLS_CC) == SUCCESS) {
+			if (EX(object)) {
+				Z_ADDREF_P(EX(object));
+			}
+			zval_dtor(free_op2.var);
+			ZEND_VM_NEXT_OPCODE();
+		}
+
 		if (Z_TYPE_P(function_name) != IS_STRING) {
 			zend_error_noreturn(E_ERROR, "Function name must be a string");
 		}
@@ -1031,6 +1049,15 @@ static int ZEND_FASTCALL  ZEND_INIT_FCAL
 	} else {
 		function_name = _get_zval_ptr_var(&opline->op2, EX(Ts), &free_op2 TSRMLS_CC);
 
+		if (Z_TYPE_P(function_name) == IS_OBJECT &&
+			zend_get_closure(function_name, &EX(called_scope), &EX(fbc), &EX(object), NULL TSRMLS_CC) == SUCCESS) {
+			if (EX(object)) {
+				Z_ADDREF_P(EX(object));
+			}
+			if (free_op2.var) {zval_ptr_dtor(&free_op2.var);};
+			ZEND_VM_NEXT_OPCODE();
+		}
+
 		if (Z_TYPE_P(function_name) != IS_STRING) {
 			zend_error_noreturn(E_ERROR, "Function name must be a string");
 		}
@@ -1155,6 +1182,15 @@ static int ZEND_FASTCALL  ZEND_INIT_FCAL
 	} else {
 		function_name = _get_zval_ptr_cv(&opline->op2, EX(Ts), BP_VAR_R TSRMLS_CC);
 
+		if (Z_TYPE_P(function_name) == IS_OBJECT &&
+			zend_get_closure(function_name, &EX(called_scope), &EX(fbc), &EX(object), NULL TSRMLS_CC) == SUCCESS) {
+			if (EX(object)) {
+				Z_ADDREF_P(EX(object));
+			}
+
+			ZEND_VM_NEXT_OPCODE();
+		}
+
 		if (Z_TYPE_P(function_name) != IS_STRING) {
 			zend_error_noreturn(E_ERROR, "Function name must be a string");
 		}
@@ -2875,6 +2911,21 @@ static int ZEND_FASTCALL  ZEND_DECLARE_C
 	ZEND_VM_NEXT_OPCODE();
 }
 
+static int ZEND_FASTCALL  ZEND_DECLARE_LAMBDA_FUNCTION_SPEC_CONST_CONST_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
+{
+	zend_op *opline = EX(opline);
+	zend_op_array *op_array;
+
+	if (zend_hash_quick_find(EG(function_table), Z_STRVAL(opline->op1.u.constant), Z_STRLEN(opline->op1.u.constant), Z_LVAL(opline->op2.u.constant), (void *) &op_array) == FAILURE ||
+	    op_array->type != ZEND_USER_FUNCTION) {
+		zend_error_noreturn(E_ERROR, "Base lambda function for closure not found");
+	}
+
+	zend_create_closure(&EX_T(opline->result.u.var).tmp_var, op_array, EG(scope), EG(This) TSRMLS_CC);
+
+	ZEND_VM_NEXT_OPCODE();
+}
+
 static int ZEND_FASTCALL  ZEND_ADD_SPEC_CONST_TMP_HANDLER(ZEND_OPCODE_HANDLER_ARGS)
 {
 	zend_op *opline = EX(opline);
@@ -33508,6 +33559,31 @@ void zend_init_opcodes_handlers(void)
   	ZEND_JMP_SET_SPEC_CV_HANDLER,
   	ZEND_JMP_SET_SPEC_CV_HANDLER,
   	ZEND_JMP_SET_SPEC_CV_HANDLER,
+  	ZEND_DECLARE_LAMBDA_FUNCTION_SPEC_CONST_CONST_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
+  	ZEND_NULL_HANDLER,
   	ZEND_NULL_HANDLER
   };
   zend_opcode_handlers = (opcode_handler_t*)labels;
Index: Zend/zend_vm_opcodes.h
===================================================================
RCS file: /repository/ZendEngine2/zend_vm_opcodes.h,v
retrieving revision 1.42.2.17.2.1.2.6
diff -u -p -d -r1.42.2.17.2.1.2.6 zend_vm_opcodes.h
--- Zend/zend_vm_opcodes.h	28 Mar 2008 14:35:01 -0000	1.42.2.17.2.1.2.6
+++ Zend/zend_vm_opcodes.h	30 Jun 2008 19:31:52 -0000
@@ -152,3 +152,4 @@
 #define ZEND_HANDLE_EXCEPTION                149
 #define ZEND_USER_OPCODE                     150
 #define ZEND_JMP_SET                         152
+#define ZEND_DECLARE_LAMBDA_FUNCTION         153
Index: Zend/tests/closure_001.phpt
===================================================================
RCS file: Zend/tests/closure_001.phpt
diff -N Zend/tests/closure_001.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_001.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,34 @@
+--TEST--
+Closure 001: Lambda without lexical variables
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+
+$lambda1 = function () {
+	echo "Hello World!\n";
+};
+
+$lambda2 = function ($x) {
+	echo "Hello $x!\n";
+};
+
+var_dump(is_callable($lambda1));
+var_dump(is_callable($lambda2));
+$lambda1();
+$lambda2("Universe");
+call_user_func($lambda1);
+call_user_func($lambda2, "Universe");
+
+echo "Done\n";
+?>
+--EXPECT--	
+bool(true)
+bool(true)
+Hello World!
+Hello Universe!
+Hello World!
+Hello Universe!
+Done
Index: Zend/tests/closure_002.phpt
===================================================================
RCS file: Zend/tests/closure_002.phpt
diff -N Zend/tests/closure_002.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_002.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,33 @@
+--TEST--
+Closure 002: Lambda with lexical variables (global scope)
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+
+$x = 4;
+
+$lambda1 = function () use ($x) {
+	echo "$x\n";
+};
+
+$lambda2 = function () use (&$x) {
+	echo "$x\n";
+};
+
+$lambda1();
+$lambda2();
+$x++;
+$lambda1();
+$lambda2();
+
+echo "Done\n";
+?>
+--EXPECT--
+4
+4
+4
+5
+Done
Index: Zend/tests/closure_003.phpt
===================================================================
RCS file: Zend/tests/closure_003.phpt
diff -N Zend/tests/closure_003.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_003.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,37 @@
+--TEST--
+Closure 003: Lambda with lexical variables (local scope)
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+
+function run () {
+	$x = 4;
+
+	$lambda1 = function () use ($x) {
+		echo "$x\n";
+	};
+
+	$lambda2 = function () use (&$x) {
+		echo "$x\n";
+	};
+
+	$lambda1();
+	$lambda2();
+	$x++;
+	$lambda1();
+	$lambda2();
+}
+
+run();
+
+echo "Done\n";
+?>
+--EXPECT--
+4
+4
+4
+5
+Done
Index: Zend/tests/closure_004.phpt
===================================================================
RCS file: Zend/tests/closure_004.phpt
diff -N Zend/tests/closure_004.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_004.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,39 @@
+--TEST--
+Closure 004: Lambda with lexical variables (scope lifetime)
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+
+function run () {
+	$x = 4;
+
+	$lambda1 = function () use ($x) {
+		echo "$x\n";
+	};
+
+	$lambda2 = function () use (&$x) {
+		echo "$x\n";
+		$x++;
+	};
+
+	return array($lambda1, $lambda2);
+}
+
+list ($lambda1, $lambda2) = run();
+
+$lambda1();
+$lambda2();
+$lambda1();
+$lambda2();
+
+echo "Done\n";
+?>
+--EXPECT--
+4
+4
+4
+5
+Done
Index: Zend/tests/closure_005.phpt
===================================================================
RCS file: Zend/tests/closure_005.phpt
diff -N Zend/tests/closure_005.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_005.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,78 @@
+--TEST--
+Closure 005: Lambda inside class, lifetime of $this
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+
+class A {
+	private $x;
+
+	function __construct($x) {
+		$this->x = $x;
+	}
+
+	function __destruct() {
+		echo "Destroyed\n";
+	}
+
+	function getIncer($val) {
+		return function() use ($val) {
+			$this->x += $val;
+		};
+	}
+
+	function getPrinter() {
+		return function() {
+			echo $this->x."\n";
+		};
+	}
+	
+	function getError() {
+		return static function() {
+			echo $this->x."\n";
+		};
+	}
+	
+	function printX() {
+		echo $this->x."\n";
+	}
+}
+
+$a = new A(3);
+$incer = $a->getIncer(2);
+$printer = $a->getPrinter();
+$error = $a->getError();
+
+$a->printX();
+$printer();
+$incer();
+$a->printX();
+$printer();
+
+unset($a);
+
+$incer();
+$printer();
+
+unset($incer);
+$printer();
+
+unset($printer);
+
+$error();
+
+echo "Done\n";
+?>
+--EXPECTF--
+3
+3
+5
+5
+7
+7
+Destroyed
+
+Fatal error: Using $this when not in object context in %sclosure_005.php on line 28
Index: Zend/tests/closure_006.phpt
===================================================================
RCS file: Zend/tests/closure_006.phpt
diff -N Zend/tests/closure_006.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_006.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,23 @@
+--TEST--
+Closure 006: Nested lambdas
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+
+$getClosure = function ($v) {
+	return function () use ($v) {
+		echo "Hello World: $v!\n";
+	};
+};
+
+$closure = $getClosure (2);
+$closure ();
+
+echo "Done\n";
+?>
+--EXPECT--
+Hello World: 2!
+Done
Index: Zend/tests/closure_007.phpt
===================================================================
RCS file: Zend/tests/closure_007.phpt
diff -N Zend/tests/closure_007.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_007.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,42 @@
+--TEST--
+Closure 007: Nested lambdas in classes
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+
+class A {
+	private $x = 0;
+
+	function getClosureGetter () {
+		return function () {
+			return function () {
+				$this->x++;
+			};
+		};
+	}
+
+	function printX () {
+		echo $this->x."\n";
+	}
+}
+
+$a = new A;
+$a->printX();
+$getClosure = $a->getClosureGetter();
+$a->printX();
+$closure = $getClosure();
+$a->printX();
+$closure();
+$a->printX();
+
+echo "Done\n";
+?>
+--EXPECT--
+0
+0
+0
+1
+Done
Index: Zend/tests/closure_008.phpt
===================================================================
RCS file: Zend/tests/closure_008.phpt
diff -N Zend/tests/closure_008.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_008.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,26 @@
+--TEST--
+Closure 008: Use in preg_replace()
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+
+function replace_spaces($text) {
+	$lambda = function ($matches) {
+		return str_replace(' ', '&nbsp;', $matches[1]).' ';
+	};
+	return preg_replace_callback('/( +) /', $lambda, $text);
+}
+
+echo replace_spaces("1 2 3\n");
+echo replace_spaces("1  2  3\n");
+echo replace_spaces("1   2   3\n");
+echo "Done\n";
+?>
+--EXPECT--
+1 2 3
+1&nbsp; 2&nbsp; 3
+1&nbsp;&nbsp; 2&nbsp;&nbsp; 3
+Done
Index: Zend/tests/closure_009.phpt
===================================================================
RCS file: Zend/tests/closure_009.phpt
diff -N Zend/tests/closure_009.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_009.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,35 @@
+--TEST--
+Closure 009: Use in preg_replace()
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+$a = 1;
+$x = function ($x) use ($a) {
+  static $n = 0;
+  $n++;
+  $a = $n.':'.$a;
+  echo $x.':'.$a."\n";
+};
+$y = function ($x) use (&$a) {
+  static $n = 0;
+  $n++;
+  $a = $n.':'.$a;
+  echo $x.':'.$a."\n";
+};
+$x(1);
+$x(2);
+$x(3);
+$y(4);
+$y(5);
+$y(6);
+?>
+--EXPECT--
+1:1:1
+2:2:1
+3:3:1
+4:1:1
+5:2:1:1
+6:3:2:1:1
Index: Zend/tests/closure_010.phpt
===================================================================
RCS file: Zend/tests/closure_010.phpt
diff -N Zend/tests/closure_010.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_010.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,22 @@
+--TEST--
+Closure 010: Closure calls itself
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+$i = 3;
+$lambda = function ($lambda) use (&$i) {
+    if ($i==0) return;
+    echo $i--."\n";
+    $lambda($lambda);
+};
+$lambda($lambda);
+echo "$i\n";
+?>
+--EXPECT--
+3
+2
+1
+0
Index: Zend/tests/closure_011.phpt
===================================================================
RCS file: Zend/tests/closure_011.phpt
diff -N Zend/tests/closure_011.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_011.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,18 @@
+--TEST--
+Closure 011: Lexical copies not static in closure
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+$i = 1;
+$lambda = function () use ($i) {
+    return ++$i;
+};
+$lambda();
+echo $lambda()."\n";
+//early prototypes gave 3 here because $i was static in $lambda
+?>
+--EXPECT--
+2
Index: Zend/tests/closure_012.phpt
===================================================================
RCS file: Zend/tests/closure_012.phpt
diff -N Zend/tests/closure_012.phpt
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ Zend/tests/closure_012.phpt	30 Jun 2008 19:31:52 -0000
@@ -0,0 +1,28 @@
+--TEST--
+Closure 012: Undefined lexical variables
+--SKIPIF--
+<?php 
+	if (!class_exists('Closure')) die('skip Closure support is needed');
+?>
+--FILE--
+<?php
+$lambda = function () use ($i) {
+    return ++$i;
+};
+$lambda();
+$lambda();
+var_dump($i);
+$lambda = function () use (&$i) {
+    return ++$i;
+};
+$lambda();
+$lambda();
+var_dump($i);
+?>
+--EXPECTF--
+Notice: Undefined variable: i in %sclosure_012.php on line 2
+
+Notice: Undefined variable: i in %sclosure_012.php on line 7
+NULL
+int(2)
+
Index: ext/pcre/php_pcre.c
===================================================================
RCS file: /repository/php-src/ext/pcre/php_pcre.c,v
retrieving revision 1.168.2.9.2.21.2.15
diff -u -p -d -r1.168.2.9.2.21.2.15 php_pcre.c
--- ext/pcre/php_pcre.c	1 Jun 2008 18:47:20 -0000	1.168.2.9.2.21.2.15
+++ ext/pcre/php_pcre.c	30 Jun 2008 19:31:52 -0000
@@ -1312,7 +1312,7 @@ static void preg_replace_impl(INTERNAL_F
 	}
 
 	SEPARATE_ZVAL(replace);
-	if (Z_TYPE_PP(replace) != IS_ARRAY)
+	if (Z_TYPE_PP(replace) != IS_ARRAY && (Z_TYPE_PP(replace) != IS_OBJECT || !is_callable_replace))
 		convert_to_string_ex(replace);
 	if (is_callable_replace) {
 		if (!zend_is_callable(*replace, 0, &callback_name)) {
Index: ext/reflection/php_reflection.c
===================================================================
RCS file: /repository/php-src/ext/reflection/php_reflection.c,v
retrieving revision 1.164.2.33.2.45.2.18
diff -u -p -d -r1.164.2.33.2.45.2.18 php_reflection.c
--- ext/reflection/php_reflection.c	27 Jun 2008 06:43:21 -0000	1.164.2.33.2.45.2.18
+++ ext/reflection/php_reflection.c	30 Jun 2008 19:31:52 -0000
@@ -38,6 +38,7 @@
 #include "zend_constants.h"
 #include "zend_ini.h"
 #include "zend_interfaces.h"
+#include "zend_closures.h"
 
 /* Undefine "getParameters" macro defined in "main/php3_compat.h" */
 #ifdef getParameters
@@ -1562,6 +1563,20 @@ ZEND_METHOD(reflection_function, getStat
 }
 /* }}} */
 
+/* {{{ proto public mixed ReflectionFunction::getClosure()
+   Invokes the function */
+ZEND_METHOD(reflection_function, getClosure)
+{
+	reflection_object *intern;
+	zend_function *fptr;
+	
+	METHOD_NOTSTATIC_NUMPARAMS(reflection_function_ptr, 0);
+	GET_REFLECTION_OBJECT_PTR(fptr);
+
+	zend_create_closure(return_value, fptr, NULL, NULL TSRMLS_CC);
+}
+/* }}} */
+
 /* {{{ proto public mixed ReflectionFunction::invoke(mixed* args)
    Invokes the function */
 ZEND_METHOD(reflection_function, invoke)
@@ -2290,6 +2305,34 @@ ZEND_METHOD(reflection_method, __toStrin
 }
 /* }}} */
 
+/* {{{ proto public mixed ReflectionMethod::getClosure([mixed object])
+   Invokes the function */
+ZEND_METHOD(reflection_method, getClosure)
+{
+	reflection_object *intern;
+	zval *obj;
+	zend_function *mptr;
+	
+	METHOD_NOTSTATIC(reflection_method_ptr);
+	GET_REFLECTION_OBJECT_PTR(mptr);
+
+	if (mptr->common.fn_flags & ZEND_ACC_STATIC)  {
+		zend_create_closure(return_value, mptr, mptr->common.scope, NULL TSRMLS_CC);
+	} else {
+		if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "o", &obj) == FAILURE) {
+			return;
+		}
+
+		if (!instanceof_function(Z_OBJCE_P(obj), mptr->common.scope TSRMLS_CC)) {
+			_DO_THROW("Given object is not an instance of the class this method was declared in");
+			/* Returns from this function */
+		}
+
+		zend_create_closure(return_value, mptr, mptr->common.scope, obj TSRMLS_CC);
+	}
+}
+/* }}} */
+
 /* {{{ proto public mixed ReflectionMethod::invoke(mixed object, mixed* args)
    Invokes the method. */
 ZEND_METHOD(reflection_method, invoke)
@@ -4641,6 +4684,7 @@ static const zend_function_entry reflect
 	ZEND_ME(reflection_function, __toString, NULL, 0)
 	ZEND_ME(reflection_function, export, arginfo_reflection_function_export, ZEND_ACC_STATIC|ZEND_ACC_PUBLIC)
 	ZEND_ME(reflection_function, isDisabled, NULL, 0)
+	ZEND_ME(reflection_function, getClosure, NULL, 0)
 	ZEND_ME(reflection_function, invoke, arginfo_reflection_function_invoke, 0)
 	ZEND_ME(reflection_function, invokeArgs, arginfo_reflection_function_invokeArgs, 0)
 	{NULL, NULL, NULL}
@@ -4684,6 +4728,7 @@ static const zend_function_entry reflect
 	ZEND_ME(reflection_method, isConstructor, NULL, 0)
 	ZEND_ME(reflection_method, isDestructor, NULL, 0)
 	ZEND_ME(reflection_method, getModifiers, NULL, 0)
+	ZEND_ME(reflection_method, getClosure, NULL, 0)
 	ZEND_ME(reflection_method, invoke, arginfo_reflection_method_invoke, 0)
 	ZEND_ME(reflection_method, invokeArgs, arginfo_reflection_method_invokeArgs, 0)
 	ZEND_ME(reflection_method, getDeclaringClass, NULL, 0)
