Avoid the call to __construct
Posted: Tue Jul 17, 2007 11:06 am
Dear community,
I've got following problem:
First of all, I'm going to describe the context. I'm developing a framework for PHP 5.1.1 and higher, for which I created a custom
data and object serialization method which does not discard references to objects which are outside the scope of an object or array to serialize
Generally that whole "mechanism" works fine. The only inconvenience is related to unserialization of an object and the unnecessary call to the constructor method, which I'd like to avoid - and that's the actual problem.
A scenario, for example:
I want a database connection class to be serializable. It has to re-establish the connection on unserialization. However, an instance of that connection class is a member of another serializable class that instantiates the connection class on construction and makes it open a connection to some DBMS.
So the very same database connection will be invoked twice on unserialization:
One time on re-creation of the "host class", and another time on unserialization of the database connection object.
As far as I can see, there is no way to avoid the call of a class constructor method, so I'm a little bit stuck in that design.
One quick and dirty solution would be, to require all serializable classes to check the first argument passed to the constructor method for being some "unserialize"-flag. But I don't like much that idea.
Does anyone have other ideas or suggestions?
I've got following problem:
First of all, I'm going to describe the context. I'm developing a framework for PHP 5.1.1 and higher, for which I created a custom
data and object serialization method which does not discard references to objects which are outside the scope of an object or array to serialize
The serialization requires all objects to serialize, to be instances of classes which implement a special interface. That interface defines public serialize and unserialize methods.http://www.php.net/serialize wrote:Circular references inside the array/object you are serialize()ing will also be stored. Any other reference will be lost.
Generally that whole "mechanism" works fine. The only inconvenience is related to unserialization of an object and the unnecessary call to the constructor method, which I'd like to avoid - and that's the actual problem.
A scenario, for example:
I want a database connection class to be serializable. It has to re-establish the connection on unserialization. However, an instance of that connection class is a member of another serializable class that instantiates the connection class on construction and makes it open a connection to some DBMS.
So the very same database connection will be invoked twice on unserialization:
One time on re-creation of the "host class", and another time on unserialization of the database connection object.
As far as I can see, there is no way to avoid the call of a class constructor method, so I'm a little bit stuck in that design.
One quick and dirty solution would be, to require all serializable classes to check the first argument passed to the constructor method for being some "unserialize"-flag. But I don't like much that idea.
Does anyone have other ideas or suggestions?