0

I have a session variable $_SESSION["customer"] which contains one object of the customer class, which has private properties such as firstname, lastname and so on. They can be accessed but public getters and setters. I don't know how to access them and use them in my code.

Here is a var_dump of $_SESSION["customer"]

object(Customer)#4 (13) { ["id":"Customer":private]=> string(4) "1019" ["fname":"Customer":private]=> string(4) "john" ["lname":"Customer":private]=> string(3) "doe" ["dob":"Customer":private]=> string(10) "1999-12-12" ["address1":"Customer":private]=> string(10) "humberwood" ["address2":"Customer":private]=> string(0) "" ["city":"Customer":private]=> string(7) "toronto" ["zip":"Customer":private]=> string(6) "m3a1c6" ["province":"Customer":private]=> string(2) "ON" ["gender":"Customer":private]=> string(1) "0" ["email":"Customer":private]=> string(21) "johndoe@humbermail.ca" ["username":"Customer":private]=> string(6) "humber" ["password":"Customer":private]=> string(6) "humber" } 

I tried using $username = $_SESSION["customer"]->Customer->getUsername(); but in vain

Shashank
  • 107
  • 3
  • 16

1 Answers1

0

You can try this, since using $_SESSION['customer'] contains a Customer object.

$customer = $_SESSION['customer'];
$username = $customer->getUsername();
Kyle Emmanuel
  • 2,193
  • 1
  • 15
  • 22
  • Throws an error `Fatal error: main(): The script tried to execute a method or access a property of an incomplete object. Please ensure that the class definition "Customer" 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` – Shashank Nov 20 '14 at 04:05
  • That must mean that object's contents are private. – Kyle Emmanuel Nov 20 '14 at 04:12
  • Try casting it @Shashank. – Kyle Emmanuel Nov 20 '14 at 04:18
  • Could you please explain @kyle – Shashank Nov 20 '14 at 04:37
  • It simply means that your Customer object is nothing but an object in its incomplete state while being stored in `$_SESSION`. refer to this: http://stackoverflow.com/questions/132194/php-storing-objects-inside-the-session/132197#132197 – Kyle Emmanuel Nov 20 '14 at 04:56
  • I am trying to workaround it by storing only the customerid in the session and then re-firing the query on my DB, but still i am unable to get the value from the session, The var_dump is `string (4) 1019`. How do I can i retrieve only 1019?? @kyle – Shashank Nov 20 '14 at 05:04
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/65252/discussion-between-kyle-emmanuel-and-shashank). – Kyle Emmanuel Nov 20 '14 at 05:09