Forked from puiutucutu/composition-and-aggregation.php
Created
March 13, 2022 09:13
-
-
Save nabtron/be59dac232815f62d805954b2b43cd2c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Existence of Composition class requires that an external object of | |
* class B is passed in on construction | |
* | |
* UML Explanation - The object at this role contains the object at the opposite role. | |
* | |
* Alternatively stated, an object of class Composition cannot be created without | |
* an object of class B (in this case through dependency injection) | |
*/ | |
class Composition { | |
private $internal_obj; | |
public function __construct(B $external_obj) { | |
$this->internal_obj = $external_obj; // requires an object of class B | |
} | |
} | |
/** | |
* Existence of the Aggregation class does not require the existence of B class, | |
* since the Aggregation class builds its own object of class B | |
* | |
* UML Explanation - Object at this role contains references to the object at the other role | |
*/ | |
class Aggregation { | |
private $internal_obj; | |
public function __construct() { | |
$this->internal_obj = new B; | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment