Serialization problem

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
cariparo
Forum Newbie
Posts: 2
Joined: Thu Nov 03, 2011 10:29 am

Serialization problem

Post by cariparo »

Hi All,
I need to save to my DB the content of a SplDoublyLinkedList variable to load it later.
It seems that php serialize() doesn't work for this kind of class, does anyone know an alternative to do that ?


Thanks in advance,
Carip
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: Serialization problem

Post by Weirdan »

I wondering what is the reason you have to use linked list instead of plain old php array in the first place? Anyways, as it doesn't implement Serializable, you'd have to get data from it and put it into a serializable container in a way you can restore your linked list from :

Code: Select all

// serialize:
$container = array();
foreach ($list as $elt) $container[] = $elt;
$data = serialize($container);

//unserialize:
$list = new SplDoublyLinkedList;
foreach (unserialize($data) as $elt) $list->push($elt);
cariparo
Forum Newbie
Posts: 2
Joined: Thu Nov 03, 2011 10:29 am

Re: Serialization problem

Post by cariparo »

Thanks
unfortunately this is not my code, i'm revamping an existing one..
As I understand the only way is to get data from it..


Thanks
Carip
Post Reply