Re: [PATCH] [RFC] Closures and lambda functions in PHP
| From: | Christian Seiler | Date: | Thu, 26 Jun 2008 21:07:57 +0000 |
| Subject: | Re: [PATCH] [RFC] Closures and lambda functions in PHP | ||
| References: | 1 2 3 4 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-38650@lists.php.net to get a copy of this message | ||
Hi!
I see exactly one problem with the patch, which is that the above script shouldn't work without "use (&$i)". I find it counterintuitive that the creation of the lambda creates a copy of $i, but all invocations of $lambda use a reference to the same $i. For n calls to $lambda, there are only 2 copies of $i (one global, one static in $lambda) where I would expect n+1 copies.Yes, you're right. My solution for this would be: -------------- zend_compile.h ------------------------- -void zend_do_fetch_static_variable(znode *varname, znode *static_assignment, int fetch_type TSRMLS_DC); +void zend_do_fetch_static_variable(znode *varname, znode *static_assignment, int fetch_type, int as_ref TSRMLS_DC); -------------- zend_compile.h ------------------------- -------------- zend_compile.c ------------------------- # in zend_do_fetch_lexical_variable: -zend_do_fetch_static_variable(varname, &value, ZEND_FETCH_STATIC TSRMLS_CC); +zend_do_fetch_static_variable(varname, &value, ZEND_FETCH_STATIC, is_ref TSRMLS_CC); ... -void zend_do_fetch_static_variable(znode *varname, znode *static_assignment, int fetch_type TSRMLS_DC) +void zend_do_fetch_static_variable(znode *varname, znode *static_assignment, int fetch_type, int as_ref TSRMLS_DC) ... - zend_do_assign_ref(NULL, &lval, &result TSRMLS_CC); + if (as_ref) { + zend_do_assign_ref(NULL, &lval, &result TSRMLS_CC); + } else { + zend_do_assign(NULL, &lval, &result TSRMLS_CC); + } # and make sure zend_do_assign can live with NULL for first param -------------- zend_compile.c ------------------------- -------------- zend_language_parser.y ------------------------- static_var_list: - static_var_list ',' T_VARIABLE { zend_do_fetch_static_variable(&$3, NULL, ZEND_FETCH_STATIC TSRMLS_CC); } - | static_var_list ',' T_VARIABLE '=' static_scalar { zend_do_fetch_static_variable(&$3, &$5, ZEND_FETCH_STATIC TSRMLS_CC); } - | T_VARIABLE { zend_do_fetch_static_variable(&$1, NULL, ZEND_FETCH_STATIC TSRMLS_CC); } - | T_VARIABLE '=' static_scalar { zend_do_fetch_static_variable(&$1, &$3, ZEND_FETCH_STATIC TSRMLS_CC); } + static_var_list ',' T_VARIABLE { zend_do_fetch_static_variable(&$3, NULL, ZEND_FETCH_STATIC, 1 TSRMLS_CC); } + | static_var_list ',' T_VARIABLE '=' static_scalar { zend_do_fetch_static_variable(&$3, &$5, ZEND_FETCH_STATIC, 1 TSRMLS_CC); } + | T_VARIABLE { zend_do_fetch_static_variable(&$1, NULL, ZEND_FETCH_STATIC, 1 TSRMLS_CC); } + | T_VARIABLE '=' static_scalar { zend_do_fetch_static_variable(&$1, &$3, ZEND_FETCH_STATIC, 1 TSRMLS_CC); } ; -------------- zend_language_parser.y ------------------------- Any objections (in case copies are wanted at all)? Regards, Christian