Jump to content

__sleep will not save private/protected variables


gamex

Recommended Posts

I am having this issue that is driving me insane. To make things worse, I tried creating a small, test script for this post so you guys could get a feel as to how I had things set up, but my little test script works the way I expect my main script to work.

 

The problem I am having is that I have protected variables in my class that I am trying to save to a session. For some reason, they will not. If I change them to public, it works fine. This is the test code I made to give you an idea on how my classes are set up:

 

abstract class GeneralCart {
protected $other_var = true;
protected $items = array();

public function __sleep(){
	return array("items");
}

public function addItem($item){
	$this->items[] = $item;
}

public function getItems(){
	return $this->items;
}
}

class Cart extends GeneralCart {
// Other stuff in here
}

class Customer {
public $cart;

public function __construct(){
	$this->cart = new Cart();
}
}

$test = new Customer();
$test->cart->addItem("ItemA");
print_r(serialize($test));

 

Has anyone ever run into this issue before?

 

I actually just did a var_dump of my actual class, and I noticed something weird:

 

["items:protected"]=>

  array(0) {

  }

 

But then if you scroll down a little bit

 

["items"]=>

  array(3) {

    // All items are here

}

 

Any ideas whats going on?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.