Jump to content

serializing objects - loses methods - the point being?


glenelkins

Recommended Posts

Hi

 

I know that serializing an object will lose the methods. But whats the point in that? What if we need the methods . Lets say the serialized string is sent from an external server where the calling function does not have access to the class file itself. then there is no way to use the methods?

Link to comment
Share on other sites

As long as the unserialized object still has access to the class it was derived from you will have access to the methods associated with that object.

 

The point of serialisation is, in the context I've used it in before, for caching large objects that do not change much over time.

 

Please be aware however, that de-serialization is very slow.

 

Check out the article below for some interesting stuff with objects and serialization.

 

http://www.devshed.com/c/a/PHP/The-Basics-of-Serializing-Objects-in-PHP/1/

Link to comment
Share on other sites

hi

 

of course i have to unserialize it. but that doesnt give access to any methods.

 

Lets take for example. On one server i have a script that returns an object of the following class:

 

class User {

    private $_username = 'John';

    public function getUsername() {

        // do something to the username here

       return $this->_username;

    }

}

 

 

The script that creates the object is called through CURL from another server, then unserializes the object

 

im left with the object , without the getUsername method and a private variable i cannot access

 

 

There must be a way around this!

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.