I have look to the composition pattern and it seems it is interessant when you want to compose with different number of leaves. In my case There are always one patrimony one project and one version for any entities.
My goal is sometimes I just use informations about a patrimony othertimes I only needs information about project.
I show you how I have design my classes, see the last lines for execution process and expected values.
I think this code doesn't work so how do I do to set variable of the parents when I instanciate a child?
// Version
class Version extends Project
private $this->_patId;
private $this->_proId;
private $this->_verId;
{
public function __contruct($patrimonyId, $projectId, $versionId)
{
$this->_patId = $patrimonyId;
$this->_proId = $projectId;
$this->_verId = $versionId;
}
public function getVersionId()
{
return $this->_verId;
}
// Project
class Project extends Patrimony
private $this->_patId;
private $this->_proId;
{
public function __contruct($patrimonyId, $projectId)
{
$this->_patId = $patrimonyId;
$this->_proId = $projectId;
}
public function getProjectId()
{
return $this->_proId;
}
// Patrimony
class Patrimony
private $this->_patId;
{
public function __contruct($patrimonyId)
{
$this->_patId = $patrimonyId;
}
public function getPatrimonyId()
{
return $this->_patId;
}
// Execution
$version = new Version(1,2,3);
$version->getVersionId(); // should return 1
$version->getProjectId(); // should return 2
$version->getPatrimonyId(); // should return 3