glenelkins Posted October 13, 2009 Share Posted October 13, 2009 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? Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 13, 2009 Share Posted October 13, 2009 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/ Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 13, 2009 Share Posted October 13, 2009 Sorry, I should expand on ym first point, to have access to the methods again you need to un-serialize the object! Quote Link to comment Share on other sites More sharing options...
glenelkins Posted October 13, 2009 Author Share Posted October 13, 2009 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! Quote Link to comment Share on other sites More sharing options...
GKWelding Posted October 13, 2009 Share Posted October 13, 2009 no, no if it is across servers as the unserialized object MUST have direct access to the classes it was derived from. Quote Link to comment Share on other sites More sharing options...
trq Posted October 13, 2009 Share Posted October 13, 2009 There isn't a way around it. Serializing saves object data, its as simple as that. If you want the methods available, the class itself needs to be within scope when the object is unserialized. Quote Link to comment Share on other sites More sharing options...
glenelkins Posted October 13, 2009 Author Share Posted October 13, 2009 fair enough! Quote Link to comment Share on other sites More sharing options...
glenelkins Posted October 13, 2009 Author Share Posted October 13, 2009 it doesnt even need to be an exact copy of the external class. i can create a local version of the class with just the methods i need and it works fine Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.