File: reference.xml

package info (click to toggle)
php-doc 20061001-1
  • links: PTS
  • area: non-free
  • in suites: etch, etch-m68k
  • size: 45,764 kB
  • ctags: 1,611
  • sloc: xml: 502,485; php: 7,645; cpp: 500; makefile: 297; perl: 161; sh: 141; awk: 28
file content (470 lines) | stat: -rw-r--r-- 12,483 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
<?xml version="1.0" encoding="iso-8859-1"?>
<!-- $Revision: 1.17 $ -->
<!-- Purpose: basic.vartype -->
<!-- Membership: bundled -->
<!-- State: experimental -->

 <reference id="ref.objaggregation">
  <title>Object Aggregation/Composition Functions</title>
  <titleabbrev>Object Aggregation</titleabbrev>

  <partintro>
    &warn.experimental;
    <section id="objaggregation.intro">
     &reftitle.intro;
     <para>
      In Object Oriented Programming, it is common to see the composition of
      simple classes (and/or instances) into a more complex one. This is a
      flexible strategy for building complicated objects and object
      hierarchies and can function as a dynamic alternative to multiple
      inheritance. There are two ways to perform class (and/or object)
      composition depending on the relationship between the composed
      elements: <emphasis>Association</emphasis> and
      <emphasis>Aggregation</emphasis>.
     </para>
     <para>
      An <emphasis>Association</emphasis> 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:
      <example>
       <title>Class association</title>
       <programlisting role="php">
<![CDATA[
<?php
class DateTime {
   
   function DateTime() 
   {
       // empty constructor
   }

   function now() 
   {
       return date("Y-m-d H:i:s");
   }
}

class Report {
   var $_dt;
   // more properties ...

   function Report() 
   {
       $this->_dt = new DateTime();
       // initialization code ...
   }

   function generateReport() 
   {
       $dateTime = $this->_dt->now();
       // more code ...
   }

   // more methods ...
}

$rep = new Report();
?>
]]>
       </programlisting>
      </example>
      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:
      <example>
       <title>Object association</title>
       <programlisting role="php">
<![CDATA[
<?php
class DateTime {
   // same as previous example
}

class DateTimePlus {
   var $_format;
   
   function DateTimePlus($format="Y-m-d H:i:s") 
   {
       $this->_format = $format;
   }

   function now() 
   {
       return date($this->_format);
   }
}

class Report {
   var $_dt;    // we'll keep the reference to DateTime here
   // more properties ...

   function Report() 
   {
       // do some initialization
   }

   function setDateTime(&$dt) 
   {
       $this->_dt =& $dt;
   }

   function generateReport() 
   {
       $dateTime = $this->_dt->now();
       // more code ...
   }

   // more methods ...
}

$rep = new Report();
$dt = new DateTime();
$dtp = new DateTimePlus("l, F j, Y (h:i:s a, T)");

// generate report with simple date for web display
$rep->setDateTime(&$dt);
echo $rep->generateReport();

// later on in the code ...

// generate report with fancy date
$rep->setDateTime(&$dtp);
$output = $rep->generateReport();
// save $output in database
// ... etc ... 
?>
]]>
       </programlisting>
      </example>
     </para>
     <para>
      <emphasis>Aggregation</emphasis>, 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.
     </para>
     <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.
     </para>
    </section>
    <section id="objaggregation.examples">
     &reftitle.examples;
     <para>
      We define 3 classes, each implementing a different storage method:
     </para>
     <para>
      <example>
       <title>storage_classes.inc</title>
       <programlisting role="php">
<![CDATA[
<?php
class FileStorage {
    var $data;

    function FileStorage($data) 
    {
        $this->data = $data;
    }
    
    function write($name) 
    {
        $fp = fopen(name, "w");
        fwrite($fp, $this->data);
        fclose($data);
    }
}

class WDDXStorage {
    var $data;
    var $version = "1.0";
    var $_id; // "private" variable

    function WDDXStorage($data) 
    {
        $this->data = $data;
        $this->_id = $this->_genID();
    }

    function store() 
    {
        if ($this->_id) {
            $pid = wddx_packet_start($this->_id);
            wddx_add_vars($pid, "this->data");
            $packet = wddx_packet_end($pid);
        } else {
            $packet = wddx_serialize_value($this->data);
        }
        $dbh = dba_open("varstore", "w", "gdbm");
        dba_insert(md5(uniqid("", true)), $packet, $dbh);
        dba_close($dbh);
    }

    // a private method
    function _genID() 
    {
        return md5(uniqid(rand(), true));
    }
}

class DBStorage {
    var $data;
    var $dbtype = "mysql";

    function DBStorage($data) 
    {
        $this->data = $data;
    }

    function save() 
    {
        $dbh = mysql_connect();
        mysql_select_db("storage", $dbh);
        $serdata = serialize($this->data);
        mysql_query("insert into vars ('$serdata',now())", $dbh);
        mysql_close($dbh);
    }
}

?>
]]>
       </programlisting>
      </example>
     </para>
     <para>
     We then instantiate a couple of objects from the defined classes, and
     perform some aggregations and deaggregations, printing some object information
     along the way:
     </para>
     <para>
      <example>
       <title>test_aggregation.php</title>
       <programlisting role="php">
<![CDATA[
<?php
include "storageclasses.inc";

// some utilty functions

function p_arr($arr) 
{
    foreach ($arr as $k => $v)
        $out[] = "\t$k => $v";
    return implode("\n", $out);
}

function object_info($obj) 
{
    $out[] = "Class: " . get_class($obj);
    foreach (get_object_vars($obj) as $var=>$val) {
        if (is_array($val)) {
            $out[] = "property: $var (array)\n" . p_arr($val);
        } else {
            $out[] = "property: $var = $val";
        }
    }
    foreach (get_class_methods($obj) as $method) {
        $out[] = "method: $method";
    }
    return implode("\n", $out);
}


$data = array(M_PI, "kludge != cruft");

// we create some basic objects
$fs = new FileStorage($data);
$ws = new WDDXStorage($data);

// print information on the objects
echo "\$fs object\n";
echo object_info($fs) . "\n";
echo "\n\$ws object\n";
echo object_info($ws) . "\n";

// do some aggregation

echo "\nLet's aggregate \$fs to the WDDXStorage class\n";
aggregate($fs, "WDDXStorage");
echo "\$fs object\n";
echo object_info($fs) . "\n";

echo "\nNow let us aggregate it to the DBStorage class\n";
aggregate($fs, "DBStorage");
echo "\$fs object\n";
echo object_info($fs) . "\n";

echo "\nAnd finally deaggregate WDDXStorage\n";
deaggregate($fs, "WDDXStorage");
echo "\$fs object\n";
echo object_info($fs) . "\n";

?>
]]>
       </programlisting>
      </example>
     </para>
     <para>
      We will now consider the output to understand some of the side-effects
      and limitation of object aggregation in PHP.
      First, the newly created <varname>$fs</varname> and <varname>$ws</varname>
      objects give the expected output (according to their respective class
      declaration). Note that for the purposes of object aggregation,
      <emphasis>private elements of a class/object begin with an underscore 
      character ("_")</emphasis>, even though there is not real distinction between
      public and private class/object elements in PHP.
     </para>
     <para>
      <informalexample>
       <programlisting>
<![CDATA[
$fs object
Class: filestorage
property: data (array)
    0 => 3.1415926535898
    1 => kludge != cruft
method: filestorage
method: write

$ws object
Class: wddxstorage
property: data (array)
    0 => 3.1415926535898
    1 => kludge != cruft
property: version = 1.0
property: _id = ID::9bb2b640764d4370eb04808af8b076a5
method: wddxstorage
method: store
method: _genid
]]>
       </programlisting>
      </informalexample>
     </para>
     <para>
      We then aggregate <varname>$fs</varname> with the
      <classname>WDDXStorage</classname> class, and print out the object
      information. We can see now that even though nominally the
      <varname>$fs</varname> object is still of
      <classname>FileStorage</classname>, it now has the property
      <varname>$version</varname>, and the method <function>store</function>,
      both defined in <classname>WDDXStorage</classname>. One important thing
      to note is that it has not aggregated the private elements defined in
      the class, which are present in the <varname>$ws</varname> object. Also
      absent is the constructor from <classname>WDDXStorage</classname>, which
      will not be logical to aggegate.
     </para>
     <para>
      <informalexample>
       <programlisting>
<![CDATA[
Let's aggregate $fs to the WDDXStorage class
$fs object
Class: filestorage
property: data (array)
    0 => 3.1415926535898
    1 => kludge != cruft
property: version = 1.0
method: filestorage
method: write
method: store
]]>
       </programlisting>
      </informalexample>
     </para>
     <para>
      The process of aggregation is cumulative, so when we aggregate
      <varname>$fs</varname> with the class <classname>DBStorage</classname>,
      generating an object that can use the storage methods of all the
      defined classes.
     </para>
     <para>
      <informalexample>
       <programlisting>
<![CDATA[
Now let us aggregate it to the DBStorage class
$fs object
Class: filestorage
property: data (array)
    0 => 3.1415926535898
    1 => kludge != cruft
property: version = 1.0
property: dbtype = mysql
method: filestorage
method: write
method: store
method: save
]]>
       </programlisting>
      </informalexample>
     </para>
     <para>
      Finally, the same way we aggregated properties and methods dynamically,
      we can also deaggregate them from the object. So, if we deaggregate the
      class <classname>WDDXStorage</classname> from <varname>$fs</varname>, we
      will obtain:
     </para>
     <para>
      <informalexample>
       <programlisting>
<![CDATA[
And deaggregate the WDDXStorage methods and properties
$fs object
Class: filestorage
property: data (array)
    0 => 3.1415926535898
    1 => kludge != cruft
property: dbtype = mysql
method: filestorage
method: write
method: save
]]>
       </programlisting>
      </informalexample>
     </para>
     <para>
      One point that we have not mentioned above, is that the process of
      aggregation will not override existing properties or methods in the
      objects. For example, the class <classname>FileStorage</classname> defines a
      <varname>$data</varname> property, and the class
      <classname>WDDXStorage</classname> also defines a similar property 
      which will not override the one in the object acquired during
      instantiation from the class <classname>FileStorage</classname>.
     </para>
    </section>
  </partintro>

  &reference.objaggregation.functions;
 </reference>
<!-- Keep this comment at the end of the file
Local variables:
mode: sgml
sgml-omittag:t
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
indent-tabs-mode:nil
sgml-parent-document:nil
sgml-default-dtd-file:"../../../manual.ced"
sgml-exposed-tags:nil
sgml-local-catalogs:nil
sgml-local-ecat-files:nil
End:
vim600: syn=xml fen fdm=syntax fdl=2 si
vim: et tw=78 syn=sgml
vi: ts=1 sw=1
-->