简体   繁体   English

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

[英]php var_dump shows array but can't access it

Can't access array but var_dump() sees it. 无法访问数组,但是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"]); shows: 说明:

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 ?" 
}

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

I don't really understand that. 我不太明白。

I got this as error message: 我收到此作为错误消息:

PHP Notice: main(): The script tried to execute a method or access a property of an incomplete object. PHP注意:main():脚本试图执行方法或访问不完整对象的属性。 Please ensure that the class definition "Question" of the object you are trying to operate on was loaded before unserialize() gets called or provide a __autoload() function to load the class definition 请确保在调用unserialize() 之前加载了您要操作的对象的类定义“问题”,或者提供了__autoload()函数来加载类定义

Thanks for your help. 谢谢你的帮助。

You need to run session_start() before you try to interact with the session object. 在尝试与会话对象进行交互之前,需要运行session_start()

Also, you need to define your object before you call session_start(), or else the PHP's session handler won't know how to deserialise the object. 另外,您需要在调用session_start()之前定义对象,否则PHP的会话处理程序将不知道如何反序列化对象。 That's why you see __PHP_Incomplete_Class . 这就是为什么您看到__PHP_Incomplete_Class的原因。

EDIT: I found this answer that may help you PHP __PHP_Incomplete_Class Object with my $_SESSION data 编辑:我发现此答案可以帮助您$ __SESSION数据与PHP __PHP_Incomplete_Class对象

I had to place session_start after my class definitions. 我必须将session_start放在类定义之后。 Thanks to Progrock 感谢Progrock

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

    session_start();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM