Home  >  Article  >  Backend Development  >  php数组转码的有关问题

php数组转码的有关问题

WBOY
WBOYOriginal
2016-06-13 13:33:27741browse

php数组转码的问题
public function array_iconv($in_charset,$out_charset,$arr){
return eval('return '.iconv($in_charset,$out_charset,var_export($arr,true).';'));
}
我在转码的时候出现这个问题
Fatal error: Call to undefined method stdClass::__set_state() in D:\soft\apache\htdocs\webtest\arrayiconv.func.php(5) : eval()'d code on line 1

------解决方案--------------------
手册中有:

_set_state()

static object __set_state ( array $properties )
当调用var_export()时,这个静态 方法会被调用(自PHP 5.1.0起有效)。

本方法的唯一参数是一个数组,其中包含按array('property' => value, ...)格式排列的类属性。

Example #4 使用 __set_state()> (PHP 5.1.0及更高版本支持)

PHP code
<?php class A
{
    public $var1;
    public $var2;

    public static function __set_state($an_array) // As of PHP 5.1.0
    {
        $obj = new A;
        $obj->var1 = $an_array['var1'];
        $obj->var2 = $an_array['var2'];
        return $obj;
    }
}

$a = new A;
$a->var1 = 5;
$a->var2 = 'foo';

eval('$b = ' . var_export($a, true) . ';'); // $b = A::__set_state(array(
                                            //    'var1' => 5,
                                            //    'var2' => 'foo',
                                            // ));
var_dump($b);

?> <div class="clear">
                 
              
              
        
            </div>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn