异常:不允许“关闭”的序列化 [英] Exception: Serialization of 'Closure' is not allowed

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

问题描述

所以我不知道我必须告诉你们,如果你需要更多的代码,请不要犹豫,问:

所以这个方法将在我们的应用程序中为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){
//仅当dev或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;
}

您可以看到所关闭的关闭。当我运行任何测试使用这段代码:

 异常:不允许序列化'Closure'

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



为了澄清上述内容,所有的做法是说我们发送的任何电子邮件都是要存储有关该电子邮件位于文件中/ tmp / mail /目录中的文件夹中。

解决方案

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

示例

  $ function = function(){
返回ABC;
};
serialize($ function); //会抛出错误

从您的代码中使用Closure

  $ callback = function()// <----------------------发行
{
return'ZendMail_'。微时(真) 的.tmp;
};

解决方案1:使用正常功能替换示例

  function emailCallback(){
return'ZendMail_'。微时(真) 的.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 //确保我们有一些默认值使用
71 if(!isset($ options ['path'])){
72 $ options ['path'] = sys_get_temp_dir();
73}
74 if(!isset($ options ['callback'])){
75 $ options ['callback'] = array($ this,'defaultCallback'); < - 这里
76}
77
78 $ this-> setOptions($ options);
79}

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

  $ callback = array($ this,aMethodInYourClass); 


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:

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.

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.

Example

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

From your code you are using Closure

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

Solution 1 : Replace with with a normal function Example

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

Solution 2 : Indirect method call by array variable

If you look at 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");

这篇关于异常:不允许“关闭”的序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆