1

I have 3 source files as follows:

first one is source_class.php

class MacMini
{
    public $memory = "16 Gigabytes";
    public $cpu = "Intel Core i7 @ 2.6GHz";
    public $HD = "1TB @ 5200 rpms";
    public function mem()
    {
        return $this->memory;
    }
    public function centralPU()
    {
        return $this->cpu;
    }
    public function hard_drive()
    {
        return $this->HD;
    }
}

///////////////////////////////////////////////////

second one is serialize.php

include "source_class.php";
$myMini = new MacMini;
$myMini->cpu = "Intel Core i7 @ 3.4GHz";
$serialized = serialize($myMini);
file_put_contents("store", $serialized);

//////////////////////////////////////////////

third one is unserialize.php

include "source_class.php";
$data = file_get_contents("store");
$unserialized = unserialize($data);

$myMini = new MacMini;
echo $myMini->cpu;

it produces the following output: "Intel Core i7 @ 2.6GHz"

Why, if the property of the cpu was changed in the serialize.php file, is it not reflected in the unserialization? I checked the raw data contents of the serialized file, "store" and the cpu property is reflected in the serialized file but when it's unserialized in unserialize.php the property change is not reflected. Why is that? Can anyone explain?

1 Answer 1

6

The problem is that you create a new $myMini object and output the cpu properity of this newly created object. The serialization/ unserialization doesn't make sense this way.

Change unserialize.php to :

include "source_class.php";
$data = file_get_contents("store");
$myMini = unserialize($data);

echo $myMini->cpu;
Sign up to request clarification or add additional context in comments.

2 Comments

Ooh now I understand. I should've caught the logic of why. Thanks!
You are welcome. This is the sort of errors because one should compare delivered hardware against ordered hardware when purchasing online. :) hehe

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.