How to implement Association,Aggregation and Composition in PHP?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • solutionwand
    New Member
    • Dec 2012
    • 16

    How to implement Association,Aggregation and Composition in PHP?

    Hi Bytes Community,

    How to implement Association , aggregation and composition in php. Please explain with complete example.

    Code:
    <?php 
    
    // composition and aggregation demo
    class Car{
    	
    	function __construct() {
    	   $engineObj = new Engine();
           print "In constructor >> Car <br/>";
       }
    
       function __destruct() {
           print "Destroying >> Car<br/> ";
       }
    	
    	}
    class Engine{
    
      function __construct() {
    	   
           print "In constructor >> Engine <br/>";
       }
    
       function __destruct() {
           print "Destroying >> Engine <br/>";
       }
    }
    
    
    
    
    ?>
    and I called this in index.php file like this.

    Code:
    <?php 
    include('classes/objects.php');
    
    //$engineObject = new Engine();
    $carObject = new Car();
    
    ?>
    its output comes.
    Code:
    In constructor >> Engine 
    In constructor >> Car 
    Destroying >> Engine 
    Destroying >> Car
    I have this code, but little confused.
    Technically in case of COMPOSITION its out put should not be like given ?
    Code:
    In constructor >> Engine 
    In constructor >> Car 
    Destroying >> Car
    Destroying >> Engine
    because If car shall destroy then Engine shall destroy. Please solve this mystery.
    Thanks
Working...