<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="content-type" content="text/html; charset=UTF-8"> <title>Object Aggregation examples</title> </head> <body><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="objaggregation.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="objaggregation.examples2.html">Examples</a></div> <div class="up"><a href="objaggregation.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div><hr /><div id="objaggregation.examples.association" class="section"> <h2 class="title">Object Aggregation examples</h2> <p class="para"> An <em class="emphasis">Association</em> is a composition of independently constructed and externally visible parts. When we associate classes or objects, each one keeps a reference to the ones it is associated with. When we associate classes statically, one class will contain a reference to an instance of the other class. For example: <div class="example" id="example-5120"> <p><strong>Example #1 Class association</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyDateTime </span><span style="color: #007700">{<br /> <br /> function </span><span style="color: #0000BB">MyDateTime</span><span style="color: #007700">() <br /> {<br /> </span><span style="color: #FF8000">// empty constructor<br /> </span><span style="color: #007700">}<br /><br /> function </span><span style="color: #0000BB">now</span><span style="color: #007700">() <br /> {<br /> return </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #DD0000">"Y-m-d H:i:s"</span><span style="color: #007700">);<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">Report </span><span style="color: #007700">{<br /> var </span><span style="color: #0000BB">$_dt</span><span style="color: #007700">;<br /> </span><span style="color: #FF8000">// more properties ...<br /><br /> </span><span style="color: #007700">function </span><span style="color: #0000BB">Report</span><span style="color: #007700">() <br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">_dt </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyDateTime</span><span style="color: #007700">();<br /> </span><span style="color: #FF8000">// initialization code ...<br /> </span><span style="color: #007700">}<br /><br /> function </span><span style="color: #0000BB">generateReport</span><span style="color: #007700">() <br /> {<br /> </span><span style="color: #0000BB">$dateTime </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">_dt</span><span style="color: #007700">-></span><span style="color: #0000BB">now</span><span style="color: #007700">();<br /> </span><span style="color: #FF8000">// more code ...<br /> </span><span style="color: #007700">}<br /><br /> </span><span style="color: #FF8000">// more methods ...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">$rep </span><span style="color: #007700">= new </span><span style="color: #0000BB">Report</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> We can also associate instances at runtime by passing a reference in a constructor (or any other method), which allow us to dynamically change the association relationship between objects. We will modify the example above to illustrate this point: <div class="example" id="example-5121"> <p><strong>Example #2 Object association</strong></p> <div class="example-contents"> <div class="phpcode"><code><span style="color: #000000"> <span style="color: #0000BB"><?php<br /></span><span style="color: #007700">class </span><span style="color: #0000BB">MyDateTime </span><span style="color: #007700">{<br /> </span><span style="color: #FF8000">// same as previous example<br /></span><span style="color: #007700">}<br /><br />class </span><span style="color: #0000BB">MyDateTimePlus </span><span style="color: #007700">{<br /> var </span><span style="color: #0000BB">$_format</span><span style="color: #007700">;<br /> <br /> function </span><span style="color: #0000BB">MyDateTimePlus</span><span style="color: #007700">(</span><span style="color: #0000BB">$format</span><span style="color: #007700">=</span><span style="color: #DD0000">"Y-m-d H:i:s"</span><span style="color: #007700">) <br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">_format </span><span style="color: #007700">= </span><span style="color: #0000BB">$format</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">now</span><span style="color: #007700">() <br /> {<br /> return </span><span style="color: #0000BB">date</span><span style="color: #007700">(</span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">_format</span><span style="color: #007700">);<br /> }<br />}<br /><br />class </span><span style="color: #0000BB">Report </span><span style="color: #007700">{<br /> var </span><span style="color: #0000BB">$_dt</span><span style="color: #007700">; </span><span style="color: #FF8000">// we'll keep the reference to MyDateTime here<br /> // more properties ...<br /><br /> </span><span style="color: #007700">function </span><span style="color: #0000BB">Report</span><span style="color: #007700">() <br /> {<br /> </span><span style="color: #FF8000">// do some initialization<br /> </span><span style="color: #007700">}<br /><br /> function </span><span style="color: #0000BB">setMyDateTime</span><span style="color: #007700">(&</span><span style="color: #0000BB">$dt</span><span style="color: #007700">) <br /> {<br /> </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">_dt </span><span style="color: #007700">=& </span><span style="color: #0000BB">$dt</span><span style="color: #007700">;<br /> }<br /><br /> function </span><span style="color: #0000BB">generateReport</span><span style="color: #007700">() <br /> {<br /> </span><span style="color: #0000BB">$dateTime </span><span style="color: #007700">= </span><span style="color: #0000BB">$this</span><span style="color: #007700">-></span><span style="color: #0000BB">_dt</span><span style="color: #007700">-></span><span style="color: #0000BB">now</span><span style="color: #007700">();<br /> </span><span style="color: #FF8000">// more code ...<br /> </span><span style="color: #007700">}<br /><br /> </span><span style="color: #FF8000">// more methods ...<br /></span><span style="color: #007700">}<br /><br /></span><span style="color: #0000BB">$rep </span><span style="color: #007700">= new </span><span style="color: #0000BB">Report</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$dt </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyDateTime</span><span style="color: #007700">();<br /></span><span style="color: #0000BB">$dtp </span><span style="color: #007700">= new </span><span style="color: #0000BB">MyDateTimePlus</span><span style="color: #007700">(</span><span style="color: #DD0000">"l, F j, Y (h:i:s a, T)"</span><span style="color: #007700">);<br /><br /></span><span style="color: #FF8000">// generate report with simple date for web display<br /></span><span style="color: #0000BB">$rep</span><span style="color: #007700">-></span><span style="color: #0000BB">setMyDateTime</span><span style="color: #007700">(</span><span style="color: #0000BB">$dt</span><span style="color: #007700">);<br />echo </span><span style="color: #0000BB">$rep</span><span style="color: #007700">-></span><span style="color: #0000BB">generateReport</span><span style="color: #007700">();<br /><br /></span><span style="color: #FF8000">// later on in the code ...<br /><br />// generate report with fancy date<br /></span><span style="color: #0000BB">$rep</span><span style="color: #007700">-></span><span style="color: #0000BB">setMyDateTime</span><span style="color: #007700">(</span><span style="color: #0000BB">$dtp</span><span style="color: #007700">);<br /></span><span style="color: #0000BB">$output </span><span style="color: #007700">= </span><span style="color: #0000BB">$rep</span><span style="color: #007700">-></span><span style="color: #0000BB">generateReport</span><span style="color: #007700">();<br /></span><span style="color: #FF8000">// save $output in database<br />// ... etc ... <br /></span><span style="color: #0000BB">?></span> </span> </code></div> </div> </div> </p> <p class="para"> <em class="emphasis">Aggregation</em>, on the other hand, implies encapsulation (hidding) of the parts of the composition. We can aggregate classes by using a (static) inner class (PHP does not yet support inner classes), in this case the aggregated class definition is not accessible, except through the class that contains it. The aggregation of instances (object aggregation) involves the dynamic creation of subobjects inside an object, in the process, expanding the properties and methods of that object. </p> <p class="para"> Object aggregation is a natural way of representing a whole-part relationship, (for example, molecules are aggregates of atoms), or can be used to obtain an effect equivalent to multiple inheritance, without having to permanently bind a subclass to two or more parent classes and their interfaces. In fact object aggregation can be more flexible, in which we can select what methods or properties to "inherit" in the aggregated object. </p> </div><hr /><div class="manualnavbar" style="text-align: center;"> <div class="prev" style="text-align: left; float: left;"><a href="objaggregation.examples.html">Examples</a></div> <div class="next" style="text-align: right; float: right;"><a href="objaggregation.examples2.html">Examples</a></div> <div class="up"><a href="objaggregation.examples.html">Examples</a></div> <div class="home"><a href="index.html">PHP Manual</a></div> </div></body></html>