How to deprecate properties of the God Object (refactoring the Blob pattern)
First actionable step to clean up a Blob is to remove deprecated properties. Consider object like this:
$godObject = new stdClass();
…
$godObject->db = new db_connection();
$godObject->db_new = new db_connection();
…
// 140 properties more…
…$godObject->user = new User();
$godObject->page = new Page();
…
$godObject->publicPath = '…/some/path';
Let’s try to remove $godObject->db
property. I can just search for it in the code base and replace all occurrences with $godObject->db_new
. But I’m not any good in manual changing some code though out a large code base, so I want to act in two steps:
1. replace all found occurrences,
2. set up an alert about deprecated property read/write access, so I can fix any missed dependencies.
Listen to property access with __set/__get
methods
Works fine (check yourself), but there are some concerns about performance degradation of the temporary solution:
But would it be an issue for a real code?
Drop-in replacement stdClass
with a custom DeprecateProperties
class
After adding class inline into the file and switch into the new class I’ve found several errors:
Fatal error: Cannot assign by reference to overloaded object in …
Derick Rethans thinks that passing property by reference didn’t really work:
Because
__get()
returns a read-only variable assigning a reference can not work as that requires a variable to be in read/write mode. Most likely the code didn’t work properly in PHP 5.1 either but luckily this is still in an unreleased component. However, I am not aware of a work-around here.
I’ve removed two occurrences of the assigning be references a run the code on the development environment — there was no any noticeable performance downgrade. Yay!