Doctrine — Doctrine 2: Give me my constructor back

John Wage writes; At Con­Foo 2010 dur­ing my pre­sen­ta­tion, some­one asked about the con­struc­tor of enti­ties in Doc­trine 2 and whether or not it could be used. I think this is some­thing worth writ­ing about since in Doc­trine 1 this was not pos­si­ble. The con­struc­tor was hi-jacked from you and used inter­nally by Doctrine.

In Doc­trine 2 it is pos­si­ble to define the con­struc­tor in your entity classes and is not required to be a zero argu­ment con­struc­tor! That's right, Doc­trine 2 never instan­ti­ates the con­struc­tor of your enti­ties so you have com­plete control!

This is pos­si­ble due to a small trick which is used by two other projects, php-object-freezer and Flow3. The gist of it is we store a pro­to­type class instance that is unse­ri­al­ized from a hand crafted seri­al­ized string where the class name is con­cate­nated into the string. The result when we unse­ri­al­ize the string is an instance of the class which is stored as a pro­to­type and cloned every­time we need a new instance dur­ing hydration.

Have a look at the method respon­si­ble for this:

Read the full arti­cle at; Doc­trine – Doc­trine 2: Give me my con­struc­tor back.

Tags:

Leave a Comment

*

Get Adobe Flash playerPlugin by wpburn.com wordpress themes