As a PHP dev, most of my projects are database-driven. I'm a fond lover of OOP & while new to the object world, I attempt to follow best practices to improve on my abilities.
My burning question
If I have an object that requires information from my database, how is that object loosely coupled because it is heavily reliant on the returning results of the database?
Code example:
<?php
class Test {
protected $repository;
public function __construct(Repository $repository)
{
$this->repository = $repository;
}
public function process()
{
$results = $this->repository->where('id', 1)->get();
( ! empty($results) ) ?: ''; // do something bad.
foreach ($results as $result) {
// This is my problem, my class now knows it needs an array key of name to be available.
// Is this bad?
echo $result['name'];
}
}
}
Here, my class is expecting an array key of name. This just feels wrong to me, because now my class is tightly coupled to the result set...
Layers
I have been doing a lot of reading on application layers, such as the domain layer, service layer, transaction scripts, application layer etc.
Even equipped with this knowledge I cannot see a solution to my problem.
To Finalise
It would be great to hear your thoughts on whether I'm driving myself mad for no reason.
It would also be great to see some code examples or further reading materials, books etc links.
Aucun commentaire:
Enregistrer un commentaire