Permalink
Browse files

[DOC] Remove $this support in closures for PHP 5.3 beta 1

  • Loading branch information...
1 parent 7cb513c commit c6d89bd4a8650ac76e14a30353d334de99e7b997 Christian Seiler committed Jan 26, 2009
@@ -1,74 +0,0 @@
---TEST--
-Closure 005: Lambda inside class, lifetime of $this
---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
@@ -1,38 +0,0 @@
---TEST--
-Closure 007: Nested lambdas in classes
---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
@@ -27,32 +27,15 @@ object(foo)#%d (%d) {
["test":"foo":private]=>
int(3)
["a"]=>
- object(Closure)#%d (2) {
- ["this"]=>
- object(foo)#%d (2) {
- ["test":"foo":private]=>
- int(3)
- ["a"]=>
- object(Closure)#%d (2) {
- ["this"]=>
- *RECURSION*
- ["static"]=>
- array(1) {
- ["a"]=>
- *RECURSION*
- }
- }
- }
+ object(Closure)#%d (1) {
["static"]=>
array(1) {
["a"]=>
&object(foo)#%d (2) {
["test":"foo":private]=>
int(3)
["a"]=>
- object(Closure)#%d (2) {
- ["this"]=>
- *RECURSION*
+ object(Closure)#%d (1) {
["static"]=>
array(1) {
["a"]=>
@@ -32,18 +32,7 @@ object(foo)#%d (1) {
["a"]=>
array(1) {
[0]=>
- object(Closure)#%d (1) {
- ["this"]=>
- object(foo)#%d (1) {
- ["a"]=>
- array(1) {
- [0]=>
- object(Closure)#%d (1) {
- ["this"]=>
- *RECURSION*
- }
- }
- }
+ object(Closure)#%d (0) {
}
}
}
@@ -52,21 +41,7 @@ int(1)
string(1) "a"
array(1) {
[0]=>
- object(Closure)#%d (1) {
- ["this"]=>
- object(foo)#%d (1) {
- ["a"]=>
- array(1) {
- [0]=>
- object(Closure)#%d (1) {
- ["this"]=>
- object(foo)#%d (1) {
- ["a"]=>
- *RECURSION*
- }
- }
- }
- }
+ object(Closure)#%d (0) {
}
}
int(1)
@@ -53,7 +53,6 @@ Array
(
[0] => Closure Object
(
- [this] =>
[parameter] => Array
(
[$param] => <required>
View
@@ -37,7 +37,6 @@
typedef struct _zend_closure {
zend_object std;
zend_function func;
- zval *this_ptr;
} zend_closure;
/* non-static since it needs to be referenced */
@@ -111,13 +110,6 @@ ZEND_API const zend_function *zend_get_closure_method_def(zval *obj TSRMLS_DC) /
}
/* }}} */
-ZEND_API zval* zend_get_closure_this_ptr(zval *obj TSRMLS_DC) /* {{{ */
-{
- zend_closure *closure = (zend_closure *)zend_object_store_get_object(obj TSRMLS_CC);
- return closure->this_ptr;
-}
-/* }}} */
-
static zend_function *zend_closure_get_method(zval **object_ptr, char *method_name, int method_len TSRMLS_DC) /* {{{ */
{
char *lc_name;
@@ -187,10 +179,6 @@ static void zend_closure_free_storage(void *object TSRMLS_DC) /* {{{ */
destroy_op_array(&closure->func.op_array TSRMLS_CC);
}
- if (closure->this_ptr) {
- zval_ptr_dtor(&closure->this_ptr);
- }
-
efree(closure);
}
/* }}} */
@@ -223,17 +211,10 @@ int zend_closure_get_closure(zval *obj, zend_class_entry **ce_ptr, zend_function
closure = (zend_closure *)zend_object_store_get_object(obj TSRMLS_CC);
*fptr_ptr = &closure->func;
- if (closure->this_ptr) {
- if (zobj_ptr) {
- *zobj_ptr = closure->this_ptr;
- }
- *ce_ptr = Z_OBJCE_P(closure->this_ptr);
- } else {
- if (zobj_ptr) {
- *zobj_ptr = NULL;
- }
- *ce_ptr = closure->func.common.scope;
+ if (zobj_ptr) {
+ *zobj_ptr = NULL;
}
+ *ce_ptr = NULL;
return SUCCESS;
}
/* }}} */
@@ -248,13 +229,6 @@ static HashTable *zend_closure_get_debug_info(zval *object, int *is_temp TSRMLS_
*is_temp = 1;
ALLOC_HASHTABLE(rv);
zend_hash_init(rv, 1, NULL, ZVAL_PTR_DTOR, 0);
- val = closure->this_ptr;
- if (!val) {
- ALLOC_INIT_ZVAL(val);
- } else {
- Z_ADDREF_P(val);
- }
- zend_symtable_update(rv, "this", sizeof("this"), (void *) &val, sizeof(zval *), NULL);
if (closure->func.type == ZEND_USER_FUNCTION && closure->func.op_array.static_variables) {
HashTable *static_variables = closure->func.op_array.static_variables;
MAKE_STD_ZVAL(val);
@@ -369,7 +343,7 @@ static int zval_copy_static_var(zval **p TSRMLS_DC, int num_args, va_list args,
}
/* }}} */
-ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_entry *scope, zval *this_ptr TSRMLS_DC) /* {{{ */
+ZEND_API void zend_create_closure(zval *res, zend_function *func TSRMLS_DC) /* {{{ */
{
zend_closure *closure;
@@ -390,19 +364,7 @@ ZEND_API void zend_create_closure(zval *res, zend_function *func, zend_class_ent
(*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->func.common.fn_flags |= ZEND_ACC_STATIC;
- closure->this_ptr = NULL;
- }
- } else {
- closure->this_ptr = NULL;
- }
+ closure->func.common.scope = NULL;
}
/* }}} */
View
@@ -30,11 +30,9 @@ void zend_register_closure_ce(TSRMLS_D);
extern ZEND_API zend_class_entry *zend_ce_closure;
-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);
+ZEND_API void zend_create_closure(zval *res, zend_function *op_array TSRMLS_DC);
ZEND_API zend_function *zend_get_closure_invoke_method(zval *obj TSRMLS_DC);
ZEND_API const zend_function *zend_get_closure_method_def(zval *obj TSRMLS_DC);
-ZEND_API zval* zend_get_closure_this_ptr(zval *obj TSRMLS_DC);
END_EXTERN_C()
View
@@ -1389,7 +1389,7 @@ void zend_do_begin_function_declaration(znode *function_token, znode *function_n
CG(labels) = NULL;
}
-void zend_do_begin_lambda_function_declaration(znode *result, znode *function_token, int return_reference, int is_static TSRMLS_DC)
+void zend_do_begin_lambda_function_declaration(znode *result, znode *function_token, int return_reference TSRMLS_DC)
{
znode function_name;
zend_op_array *current_op_array = CG(active_op_array);
@@ -1409,9 +1409,6 @@ void zend_do_begin_lambda_function_declaration(znode *result, znode *function_to
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;
}
View
@@ -434,7 +434,7 @@ void zend_do_end_function_call(znode *function_name, znode *result, const znode
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_begin_lambda_function_declaration(znode *result, znode *function_token, int return_reference 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);
@@ -647,10 +647,8 @@ expr_without_variable:
| T_ARRAY '(' array_pair_list ')' { $$ = $3; }
| '`' backticks_expr '`' { 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); }
+ | function is_reference '(' { zend_do_begin_lambda_function_declaration(&$$, &$1, $2.op_type 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:
View
@@ -4393,7 +4393,7 @@ ZEND_VM_HANDLER(153, ZEND_DECLARE_LAMBDA_FUNCTION, CONST, CONST)
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_create_closure(&EX_T(opline->result.u.var).tmp_var, op_array TSRMLS_CC);
ZEND_VM_NEXT_OPCODE();
}
View
@@ -2912,7 +2912,7 @@ static int ZEND_FASTCALL ZEND_DECLARE_LAMBDA_FUNCTION_SPEC_CONST_CONST_HANDLER(
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_create_closure(&EX_T(opline->result.u.var).tmp_var, op_array TSRMLS_CC);
ZEND_VM_NEXT_OPCODE();
}
Oops, something went wrong.

0 comments on commit c6d89bd

Please sign in to comment.