在撰写此备注时(PHP 5.3.1),不允许序列化闭包。
这意味着像下面这样的代码
<?php
$bar3 = function($a) {
return $a * 2;
};
wincache_ucache_set('foo3', $bar3);
var_dump(wincache_ucache_get('foo3'));
echo "<br>";
?>
将抛出异常
[11-Feb-2010 09:25:33] PHP Fatal error: Uncaught exception 'Exception' with message 'Serialization of 'Closure' is not allowed' in C:\inetpub\wwwroot\phptest\tests\ucache\wincache15.php:6
堆栈跟踪
#0 C:\inetpub\wwwroot\phptest\tests\ucache\wincache15.php(6): wincache_ucache_set('foo3', Object(Closure))
#1 {main}
thrown in C:\inetpub\wwwroot\phptest\tests\ucache\wincache15.php on line 6
但是,您可以像这样捕获此异常
<?php
$bar3 = function($a) {
return $a * 2;
};
try
{
wincache_ucache_set('foo3', $bar3);
}
catch (Exception $e)
{
var_dump($e->getMessage());
}
var_dump(wincache_ucache_get('foo3'));
echo "<br>";
?>
这将生成如下所示的输出
string(41) "Serialization of 'Closure' is not allowed" NULL