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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.