Good as you said is a Aggregation that is they have the same hierarchical level, being one independent of the other, but still related.
To define the best load method you will have to check your usage frequency criteria, I believe that if you constantly use data from this aggregation, it is better to keep it started already in the constructor:
require_once CONTROLLERS.'EnderecoController.php';
class Empresa {
function __construct(){
$this->endereco = new EnderecoController();
}
}
Here the controller of Endereco
is always active together with the company, ie it will always load the two files.
Now if you only use in some specific cases, be better
perform a call download:
Class App{
function __get($var){
$controller = sprintf('%sController', ucword($var));
if(!isset($this->{$var}) && file_exists(CONTROLLERS."{$controller}.php")){
$this->{$var} = new $controller;
}
if(!isset($this->{$var})){
echo "Variable {$var} not found!";
}
return $this->{$var};
}
}
Class Empresa extends App{
function AddEnderecoEmpresa(){
$this->endereco->save($cdEmpresa);
}
}
Here he will carry Endereco
only when you need to use.
Your doubt is based on the best load method for this aggregation or the construction of this aggregation?
– Guilherme Lautert
Hello, Guilherme. It is based on better load method (since it does not "use" autoloading); the relationship/aggregation in DB is already ok. My question is related in the "coupling": best practices to include and coupling with the controller (in this case, from the company).
– Andre Gomes Borges Vieira