How to deprecate properties of the God Object (refactoring the Blob pattern)

Alexander Terehov
terales engineering
2 min readJul 9, 2017

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:

Access dynamic properties via `__get/__set` is roughly 346 times slowen that without them. Screenshot from QCachegrind 0.7.4. Measured in PHP 5.6.30 with XDebug profiler.

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!

--

--