I am beginning to get into more OO PHP and also writing tests for that objects. My main question is: if I have a Blog_Post object, and I call $post->setCategory( 'Foo' ), should doing that save to the database directly?
The reason I ask is for unit testing, I often don’t want to use the DB for these kind of things because that’s not what I am testing.
I have seen people suggest doing something like
function __construct( PDO $db )
to pass in the database object to be used, then use mock when testing. However, I really don’t like the idea of having instantiate my database objects all the time by whatever is calling the Blog_Post class.
This is in the scope of WordPress, which does not have an OO approach fundamentally – with my current Blog_Post, the setter method would just call the DB (via a global $wpdb!! (i know!)).
Really I wanted to know, what’s the general pattern that is the path of least resistance with something like this. Would Blog_Post just write it’s self, or would one maybe put on a “save()” method to actually push all set properties to the DB? Or perhaps set a flag on teh object “setSaveToDB(false)” before calling the setter.
Thanks, hope this make some sense!
Short answer: No. Delegate loading, saving to another layer.