I've recently tried to learn more about OOP, but I'm just not sure about the following.
Is it bad practice to call other classes in the construct like so:
class Main{
function __construct(){
$this->db = new DatabaseConnection();
$this->rp = new ResponseHandler();
}
public function SelectUser( $user_id ){
...
$this->db->query('...') // `query` is a method in DataBaseConnection
...
return $this->rp->msg('...'); // `msg` is a method in ResponseHandler
}
In my eyes it seems like an easy and nice way to do this, but I don't know if this is actually the/a correct way of doing OOP.
If you know that the methods of your Main class will need a DatabaseConnection and a ResponseHandler to perform whatever their job is, IMHO your approach is perfectly ok.
Thus, you keep the implementation hidden from the user of your class. And that's what OOP is all about.
I don't like the idea of dependency injection via constructor because it forces everybody up the chain of callers to know what other classes your Main class needs.