PHP var_dump显示数组但无法访问它

时间:2018-07-29 17:24:47

标签: php arrays

无法访问数组,但var_dump()看到它。

session_start();

include './entities/Answer.php';
include './entities/Question.php';
include './Database.php';

 if (isset($_SESSION["question"])) {
        $correct = TRUE;
        var_dump($_SESSION["question"]);
        var_dump($_SESSION["question"]->answers);
        }
    } else {
        echo "<h2>no question selected.</h2>";
    }

var_dump($_SESSION["question"]); 显示:

object(__PHP_Incomplete_Class)#2 (8) {  
    ["__PHP_Incomplete_Class_Name"]=> string(8) "Question"  
    ["id"]=> string(1) "1" 
    ["correct"]=> NULL 
    ["wrong"]=> NULL 
    ["answers"]=> array(3) { 
        [0]=> object(__PHP_Incomplete_Class)#3 (5) { 
            ["__PHP_Incomplete_Class_Name"]=> string(6) "Answer" 
            ["correct"]=> string(1) "0" 
            ["question_id":"Answer":private]=> NULL 
            ["id"]=> string(1) "1" 
            ["text"]=> string(9) "antwort 1" 
        } 
        [1]=> object(__PHP_Incomplete_Class)#4 (5) { 
            ["__PHP_Incomplete_Class_Name"]=> string(6) "Answer" 
            ["correct"]=> string(1) "0" 
            ["question_id":"Answer":private]=> NULL 
            ["id"]=> string(1) "2" 
            ["text"]=> string(5) "ant 2" 
        }
        [2]=> object(__PHP_Incomplete_Class)#5 (5) { 
            ["__PHP_Incomplete_Class_Name"]=> string(6) "Answer" 
            ["correct"]=> string(1) "0" 
            ["question_id":"Answer":private]=> NULL 
            ["id"]=> string(1) "3" 
            ["text"]=> string(5) "ant 3"
        }
    }
    ["creator_id"]=> NULL 
    ["categories"]=> array(0) { } 
    ["text"]=> string(17) "eine Test frage ?" 
}

但是var_dump($_SESSION["question"]->answers);显示NULL

我不太了解。

我收到此错误消息:

  

PHP注意:main():该脚本试图执行方法或访问不完整对象的属性。请确保在 调用unserialize()之前已加载要尝试操作的对象的类定义“问题”,或提供__autoload()函数来加载类定义

感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

在尝试与会话对象进行交互之前,您需要运行session_start()

此外,您需要在调用session_start()之前定义对象,否则PHP的会话处理程序将不知道如何反序列化对象。这就是为什么您看到__PHP_Incomplete_Class的原因。

编辑:我发现此答案可能对您PHP __PHP_Incomplete_Class Object with my $_SESSION data

有帮助

答案 1 :(得分:0)

我必须将session_start放在类定义之后。感谢Progrock

.label{padding: 5px 10px; color: #fff;}
.red{background-color: #da4453}
.blue{background-color: #3bafda;}

<span class="label red">red label</span>
<span class="label blue">blue label</span>