Is the following implementation in PHP correct?
I focused on what they say about the lifetime of dependencies:
“Aggregation implies that an aggregate object and its owner have identical lifetimes.“
The implementation of acquaintance:
class B {}
class A
{
/**
* @var B
*/
private $b;
// $b can be injected to $a via a constructor
public function __construct(B $b)
{
$this->b = $b;
// this is not possible and would be an aggregation relationship
// $this->b = new B();
}
// $b can also be injected to $a via a setter
public function setB(B $b)
{
$this->b = $b;
}
}
The implementation of aggregation:
class B {}
class A
{
/**
* @var B
*/
private $b;
// $b is instantiated in the constructor (the lifetime of $b is the same as of $a)
public function __construct()
{
$this->b = new B();
}
}