沃梦达 / 编程问答 / php问题 / 正文

例外:不允许序列化“闭包"

Exception: Serialization of #39;Closure#39; is not allowed(例外:不允许序列化“闭包)

本文介绍了例外:不允许序列化“闭包"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

So I am not sure exactly what I would have to show you guys, how ever if you need more code please do not hesitate to ask:

所以这个方法会在我们的应用程序中为 Zend 设置 initMailer:

So this method will set up the initMailer for Zend with in our application:

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;
}

您可以看到其中的闭包.当我运行任何使用此代码的测试时,我得到:

You can see the closure that lies with in. When I run any tests that use this code I get:

Exception: Serialization of 'Closure' is not allowed 

因此与此关闭"相关的所有测试都失败了.所以我在这里问你们我该怎么做.

and thus all the tests in relation to this "closure" fails. So I am here asking you guys what I should do.

为了澄清上述内容,我们所做的只是说,我们发送的任何电子邮件都希望将有关该电子邮件的信息存储在文件中/tmp/mail/目录中的文件夹中.

For clarification on the above, all were doing is saying that any email we send out we want to store information about that email in a folder in the /tmp/mail/ directory in a file.

推荐答案

显然匿名函数不能序列化.

Apparently anonymous functions cannot be serialized.

例子

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

从你的代码中你正在使用闭包:

From your code you are using Closure:

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

解决方案一:替换成普通函数

例子

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

方案二:数组变量间接调用方法

如果您查看 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     }

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

You can use the same approach to send the callback

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

这篇关于例外:不允许序列化“闭包"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:例外:不允许序列化“闭包"

基础教程推荐