Several weeks ago, a correspondent presented a legacy situation that I’ve never had to deal with. He was working his way through Modernizing Legacy Applications in PHP, and realized the codebase was storing serialized PHP objects in a database. He couldn’t refactor the class names without seriously breaking the application.
I was carefully moving my classes to a PSR-0/4 structure when I found this. The application saves the output of
serialize($shoppingCart)in a BLOB column, and unserializes it later to recreate the ShoppingCart object. The serialized object string looks like this:
See how the class name is embedded in the serialized string? If I rename the ShopppingCart class to PSR-0/4 namespace, then the old class won’t be found when the application tries to
unserialize()the serialized representation. How can I begin refactoring this without breaking the whole application?
Before I was able to reply, my correspondent ended up changing the serialization strategy to use JSON, which was a rather large change. It ended up well, but it turns out there is a less intrusive solution:
If you’re in a serialized situation, and you need to change a class name, you can use a
class_alias() to point the old class name to the new one. (Call
class_alias() early in execution, probably before you register your autoloader.) You can then rename and move the class according to PSR-0/4 rules, and PHP will handle the rest for you.
For example, if you renamed ShoppingCart to Domain\Orders\Cart, you might do this:
Now when you call
unserialize($shoppingCart) to create an object, PHP will create it as an instance of Domain\Orders\Cart instead of ShoppingCart. Re-serialized representations of the recreated object will retain the new class name, not the old one:
As soon as there are no more serialized representations using the old class name, you can remove the
class_alias() call entirely.
Are you stuck with a legacy codebase? Are you having a hard time getting started on modernizing it? If so, I have put together an online weekend “boot camp” where we go through the entire modernization process, from basic prerequisities at the beginning to setting up a DI container at the end. After that end-to-end study of the steps involved, you should be able to jump-start your own modernizing project much more easily.
The 8-hour camp will combine lecture and “live” coding with examples. In particular, there will be plenty of opportunity for you to get answers specific to your own modernizing situation, and to chat with other attendees.
The two-day camp will be on Sat 11 July and Sun 12 July. Each day will run from 12 noon to 4pm (US Central Daylight), with time for breaks built in. That makes it 10am-2pm for Pacific time zone attendees, and 1-5pm for Eastern, which should be convenient for almost everyone.
With your ticket to attend the boot camp, you get:
- Two days of online boot camp, four hours each
- A review of the entire modernization process from beginning to end
- A bonus recording of the camp
- For $399
This might be the only time I lead this kind of boot camp, so get your ticket while there are still seats available!