Ace of Spade
Ace of Spade

Reputation: 420

php var_dump shows array but can't access it

Can't access array but var_dump() sees it.

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); shows 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. 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

Thanks for your help.

Upvotes: 1

Views: 465

Answers (2)

Ace of Spade
Ace of Spade

Reputation: 420

I had to place session_start after my class definitions. Thanks to Progrock

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

    session_start();

Upvotes: 0

Daniel
Daniel

Reputation: 1420

You need to run session_start() before you try to interact with the session object.

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. That's why you see __PHP_Incomplete_Class.

EDIT: I found this answer that may help you PHP __PHP_Incomplete_Class Object with my $_SESSION data

Upvotes: 2

Related Questions