How to access object that is stored in session in PHP?


#1

This is how I’ve stored an object in session,

if ($_REQUEST) {
        $userDataObj = new UserData();
        $userDataObj->userName = $_REQUEST['name_text'];
        $userDataObj->userEmail = $_REQUEST['email_text'];
        $userDataObj->gender = $_REQUEST['gender_radio'];
        $userDataObj->address = $_REQUEST['address_text'];
        $countryNames = new UserCountry();
        $userDataObj->country = $countryNames->getCountryName($_REQUEST['country_select']);
        $_SESSION['user_data'][] = $userDataObj;
    }

How I can access this object?


#2

That is the object, correct?

Does this look like something we can assign that object to?


#3

I have done this,
$obj = $_SESSION['user_data'][0];
If I want userName then what will be the code?


#4

You haven’t answered my question(s).


#5

We can assign a new value at the last of the array like,
$arr[] = $val;
Same syntax I’m using here.

So, ya we can…


#6

I’m seeing something new here, so, thanks for that.


#7

Actually my data is storing, here is my session data,
after writing,

foreach ($_SESSION['user_data'] as $obj) {
   print_r($obj);
}

And output is,
__PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => UserData [userName] => Ankur Singh [userEmail] => ankursingh@gmail.com [gender] => male [address] => Mahuariya [country] => India ) __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => UserData [userName] => Ankur Singh [userEmail] => ankursingh@gmail.com [gender] => male [address] => Mahuariya [country] => India ) __PHP_Incomplete_Class Object ( [__PHP_Incomplete_Class_Name] => UserData [userName] => Ankur Singh [userEmail] => ankursingh@gmail.com [gender] => male [address] => Mahuariya [country] => India )

How I can access Individual data?