反序列化后,PHP类公共变量未定义

时间:2015-05-19 00:02:58

标签: php json class

所以在某些时候,ajax调用会发送一个JSON,我将其转换为对象并存储在会话中:

    if ( $deps = json_decode( $raw_json, true ) )
    {   
        $pkgs = array();

        foreach ( $deps['vector'] as $key )
        {
            if ( strcasecmp( $key['package'], $key['component'] ) == 0 )
            {
                $package = new Package();
                $package->name = $key['package'];
                $package->version = $key['version'];
                $package->cxx_include = $key['cxx_inc'];
                $package->cxx_link = $key['cxx_link'];
                $pkgs[] = $package;
            }
            else
            {
                foreach( $pkgs as $package ) 
                {
                    if ( $package->name == $key['package'] )
                    {
                        $component = new Component();
                        $component->name = $key['component'];
                        $component->cxx_link = $key['cxx_link'];
                        $package->components[] = $component;
                    }
                }
            }
        }

        $_SESSION['pkgs'] = serialize( $pkgs );

实际的课程很简单:

class Component
{
    public $name;
    public $cxx_link;
}

class Package
{
    public $name;
    public $version;
    public $cxx_inclue;
    public $cxx_link;
    public $components = array();
}

在稍后的时候,我反序列化并尝试解析它们:

    $pkgs = isset( $_SESSION['pkgs'] ) ? unserialize($_SESSION['pkgs']) : array();

    foreach ( $pkgs as $package )
    {
        fwrite( $cmake, "find_package(" . $package->name . " " . $package->version . " " );
        if ( count( $package->components ) > 0 )
        {
            fwrite( $cmake, "COMPONENTS " );
            foreach ( $package->components as $component )
                fwrite( $cmake, $component->name. " " );

            fwrite( $cmake, "REQUIRED )\r\n" );
        }

        if ( isset( $package->cxx_include ) )
            // ERROR here: undefined variable
            fwrite( $cmake, "include_directories(${".$package->cxx_include."})\r\n" );

        if ( isset( $package->cxx_link ) )
            // ERROR here: undefined variable
            fwrite( $cmake, "target_link_libraries($rapp_name ${".$package->cxx_link."})\r\n" );
        foreach ( $package->components as $component )
        {
            if ( isset($component->cxx_link) )
                // ERROR here: undefined variable
                fwrite( $cmake, "target_link_libraries($rapp_name ${".$component->cxx_link."})\r\n" );
        }
    }

真正令我困惑的是,未定义的变量不是$ component-> cxx_link或$ package-> cxx_link,但我所显示的是JSON的实际值。

例如,如果JSON是:

{
    "vector": [
        {
            "package": "Boost",
            "version": "1.54",
            "component": "Boost",
            "cxx_inc": "Boost_INCLUDE_DIRS",
            "cxx_link": "Boost_LIBRARIES"
        },
        {
            "package": "Boost",
            "version": "1.54",
            "component": "date_time",
            "cxx_inc": "",
            "cxx_link": "Boost_DATE_TIME_LIBRARY"
        },
        {
            "package": "Boost",
            "version": "1.54",
            "component": "filesystem",
            "cxx_inc": "",
            "cxx_link": "Boost_FILESYSTEM_LIBRARY"
        },
        {
            "package": "Opencv",
            "version": "3.0",
            "component": "OpenCV",
            "cxx_inc": "OpenCV_INCLUDE_DIRS",
            "cxx_link": "OpenCV_LIBS"
        },
        {
            "package": "Eigen3",
            "version": "3.0",
            "component": "Eigen3",
            "cxx_inc": "EIGEN3_INCLUDE_DIR",
            "cxx_link": ""
        }
    ]
}

我得到了错误:

PHP Notice:  Undefined variable: .Boost_INCLUDE_DIRS.
PHP Notice:  Undefined variable: .Boost_LIBRARIES.
PHP Notice:  Undefined variable: .OpenCV_INCLUDE_DIRS.

我设置类成员的值是多少。 如果我正确地理解了这一点,那么在JSON迭代期间的赋值操作会做一些浅拷贝吗?

编辑根据Barmar的评论,我删除了serialize / unserialize,并将类定义移动到使用这些类的每个文件的顶部。我不再得到自动加载错误,但我仍然得到完全相同的未定义变量错误。

1 个答案:

答案 0 :(得分:4)

<强>修正:

所以问题是你使用双引号而不是单引号作为字符串。 PHP有一个叫变量变量的东西,你使用它是无意的(参见http://php.net/manual/en/language.variables.variable.php)。

将包含"${something}"之类的每个字符串切换为单引号(或按照Alex提到的那样转义美元符号),你就可以了,即:

fwrite( $cmake, "include_directories(${".$package->cxx_include."})\r\n" );

将是

fwrite( $cmake, 'include_directories(${'.$package->cxx_include.'})'."\r\n" );

(请注意,您无法在单引号字符串中使用\r\n。)

旧回答:

你的Package课程中有拼写错误。

您将其分配给$package->cxx_include,但实际名称缺少&#34; d&#34;。

class Package
{
    public $name;
    public $version;
    public $cxx_inclue; // typo
    public $cxx_link;
    public $components = array();
}