例外:序列化'关闭'不被允许

时间:2012-12-05 23:12:32

标签: php exception serialization closures

所以我不确定我要向你们展示什么,如果你需要更多代码,请不要犹豫,问:

因此,此方法将在我们的应用程序中为Zend设置initMailer:

protected function _initMailer()
{
    if ('testing' !==  APPLICATION_ENV) {
        $this->bootstrap('Config');
        $options = $this->getOptions();
        $mail = new Zend_Application_Resource_Mail($options['mail']);
    }elseif ('testing'  ===  APPLICATION_ENV) {
        //change the mail transport only if dev or test
        if (APPLICATION_ENV <> 'production') {

            $callback = function()
            {
                return 'ZendMail_' . microtime(true) .'.tmp';
            };

            $mail = new Zend_Mail_Transport_File(
                array('path' => '/tmp/mail/',
                        'callback'=>$callback
                )
            );

            Zend_Mail::setDefaultTransport($mail);
        }
    }


    return $mail;
}

你可以看到它所在的闭包。当我运行任何使用这段代码的测试时,我得到:

Exception: Serialization of 'Closure' is not allowed 

因此与此“闭包”相关的所有测试都失败了。所以我在这里问你们应该做些什么。

为了澄清上述情况,所有人都在说我们发送的任何电子邮件都要将有关该电子邮件的信息存储在文件的/ tmp / mail /目录下的文件夹中。

4 个答案:

答案 0 :(得分:38)

显然匿名函数无法序列化。

示例

$function = function () {
    return "ABC";
};
serialize($function); // would throw error

从您的代码中使用Closure:

$callback = function () // <---------------------- Issue
{
    return 'ZendMail_' . microtime(true) . '.tmp';
};

解决方案1:替换为普通功能

示例

function emailCallback() {
    return 'ZendMail_' . microtime(true) . '.tmp';
}
$callback = "emailCallback" ;

解决方案2:按数组变量调用间接方法

如果你看http://docs.mnkras.com/libraries_23rdparty_2_zend_2_mail_2_transport_2file_8php_source.html

   public function __construct($options = null)
   63     {
   64         if ($options instanceof Zend_Config) {
   65             $options = $options->toArray();
   66         } elseif (!is_array($options)) {
   67             $options = array();
   68         }
   69 
   70         // Making sure we have some defaults to work with
   71         if (!isset($options['path'])) {
   72             $options['path'] = sys_get_temp_dir();
   73         }
   74         if (!isset($options['callback'])) {
   75             $options['callback'] = array($this, 'defaultCallback'); <- here
   76         }
   77 
   78         $this->setOptions($options);
   79     }

您可以使用相同的方法发送回调

$callback = array($this,"aMethodInYourClass");

答案 1 :(得分:22)

PHP不允许直接关闭序列化。但你可以像PHP Super Closure一样使用powefull类:https://github.com/jeremeamia/super_closure

这个类非常简单易用,并且捆绑在队列管理器的laravel框架中。

来自github文档:

$helloWorld = new SerializableClosure(function ($name = 'World') use ($greeting) {
    echo "{$greeting}, {$name}!\n";
});

$serialized = serialize($helloWorld);

答案 2 :(得分:11)

如前所述:开箱即用的封闭无法序列化。

然而,使用__sleep()__wakeup()魔术方法和反射你可以手动使闭包可序列化。有关详细信息,请参阅extending-php-5-3-closures-with-serialization-and-reflection

这使用了反射和php函数 eval 。请注意这样就可以开启CODE注入的可能性,所以请注意你要序列化的内容。

答案 3 :(得分:3)

你必须禁用Globals

 /**
 * @backupGlobals disabled
 */
相关问题