Jump to content

How to save the array vars in a class?


cCj

Recommended Posts

Hi,
I'm using serialize to save an object which worked until I started using Arrays in the class. Please see this code below:

[code]class Base extends Tile
{
var $owner;
var $resources = Array();

function Base($owner)
{
$this->Tile();
$this->owner = $owner;
}
function __sleep()
{
  // used this before which works for simple vars like owner
    //return array('owner');
  // trying this now but doesn't seem to work for arrays
return( array_keys( get_object_vars( &$this ) ) );
}
}[/code]

Then you would just call serialize and unserialize.

But like I commented that doesn't work, the array is not saved.
Any suggestions what could I try?

Thanks.
Link to comment
Share on other sites

[quote author=poirot link=topic=103711.msg413179#msg413179 date=1155216728]
What do you mean with "the array is not saved"?
[/quote]

It's empty once unserialized. The owner var however has it's value so I was thinking that it was "not saved".
Link to comment
Share on other sites

I don't see any point in your __sleep() function.

If you run the script below it will create a Base object then save it. On subsequnt runs it pickes up the saved object (check the time values)

[code]<?php
session_start();


class Base
{
var $owner;
var $resources = Array();

function Base($owner)
{
$this->owner = $owner;
$this->resources = getdate();
}
}

if (isset($_SESSION['mybase'])) {
$obj = unserialize($_SESSION['mybase']) ;
} else {
$obj = new Base(1);
}

echo '<pre>', print_r($obj, true), '</pre>';

$_SESSION['mybase'] = serialize($obj);
?>[/code]
Link to comment
Share on other sites

Thank you Barand, your code works just like mine and I was able to find the problem now that I knew the problem was not in the serialazation.
What I was doing was something like this:

[code]
$tile = $map[$y][$x];
$tile->addToUnits($unit);
[/code]

When the working code was:
[code]
$map[$y][$x]->addToUnits($unit);
[/code]

So I had misunderstood how php handles variables, even $map had objects the assigment to $tile actually copied the object instead of passing reference to it. So I will be using & to get the reference instead of copy from now on.

Thanks.
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.