Does anyone know how this works?
Asked
Active
Viewed 4,264 times
1 Answers
29
This works by unserializing objects. Unserializing in PHP does prevent the constructor to be called as the serialized object has been already constructed.
Create an object without calling it's constructor in PHP:
$className = 'stdClass'; # set classname here
$serialized = sprintf('O:%d:"%s":0:{}', strlen($className), $className);
$object = unserialize($serialized);
For more details please see this article: Doctrine 2: Give me my constructor back
-
thx for the explanation and link. I had no idea you could create an instance of an object like this! I'm assuming that the format: "O:%d:"%s":0:{}", is the serialized format of an object? – blacktie24 Jul 02 '11 at 17:19
-
1@blacktie24: Yes it is. However if the object has properties and inheritance, it can differ. Additionally objects implementing the `serializeable` interface differ as well. – hakre Jul 02 '11 at 17:24
-
interesting, really appreciate the followup comment, I'll definitely mess around with serialized objects to see what the differences are with the variable setups. Cheers. – blacktie24 Jul 02 '11 at 17:52
-
1If you want to nicely inspect serialized data, checkout the [Serialized PHP library](https://github.com/ktomk/Serialized). It can pretty print serialized objects and data. And it can create serialized objects more fine-grained as well. – hakre Jul 03 '11 at 07:07
-
Now I have a question, how can I ask Doctrine to call the constructor before hydration when fetching the result ? I have some initialisation work in the constructor. – David Lin Jul 11 '13 at 02:15
-
2oh, I've just found it, i can use the postLoad doctrine event. cheers! – David Lin Jul 11 '13 at 02:25