In my example I'm using the PHP framework Yii2 but I think this applies to most OO languages.
I have an ActiveRecord
base class which most of my business objects extend from e.g. Project
.
At the moment if I want a Project
instance I call
Project::findOne(['id' => $id]);
findOne is a static method of ActiveRecord
(which is part of the Yii2 framework). So this is bad form because I can't easily mock/stub the return of this call when writing unit tests.
But what's the best way to get around this?
I could create a class CActiveRecord
that inherits from ActiveRecord
and wrap the static call in a non-static call and use that everywhere - but then I would have to instantiate a throw-away Project
object in order to get the actual instance. What if the Project
object needed some heavy config to be instantiated - I would be passing random nonsense into the constructor just to get an instance.
Summary: Simply changing statics to non-statics seems wrong - shouldn't I also move the functions somewhere else? If so, where?