Re: [RFC] Closures: updated proposal and patch
| From: | Stanislav Malyshev | Date: | Mon, 07 Jul 2008 04:19:36 +0000 |
| Subject: | Re: [RFC] Closures: updated proposal and patch | ||
| References: | 1 2 | Groups: | php.internals |
| Request: | Send a blank email to internals+get-38797@lists.php.net to get a copy of this message | ||
Hi!
function replace_spaces ($text) {
static $replacement = function ($matches) {
return str_replace ($matches[1], ' ', ' ').' ';
};
return preg_replace_callback ('/( +) /', $replacement, $text);
}
That is using static would result in creating the function only once.
I'm not sure this exactly one would work, as closure is not a constant expression and thus its use as initializer may not work. However, converting it to pseudo-singleton may work:
static $replacement = null;
if($replaceement == null) {
$replacement = function { blah-blah }
}
--
Stanislav Malyshev, Zend Software Architect
stas@zend.com http://www.zend.com/
(408)253-8829 MSN: stas@zend.com