Example 4. test_aggregation.php
<?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";
?>
We will now consider the output to understand some of the
side-effects and limitation of object aggregation in PHP.
First, the newly created $fs and $ws objects give the expected
output (according to their respective class declaration).
Note that for the purposes of object aggregation, private
elements of a class/object begin with an underscore character
("_"), even though there is not real distinction
between public and private class/object elements in PHP.
$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
We then aggregate $fs with the WDDXStorage class, and print
out the object information. We can see now that even though
nominally the $fs object is still of FileStorage, it now
has the property $version, and the method store(), both
defined in WDDXStorage. One important thing to note is that
it has not aggregated the private elements defined in the
class, which are present in the $ws object. Also absent
is the constructor from WDDXStorage, which will not be logical
to aggegate.
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
The proccess of aggregation is cummulative, so when we
aggregate $fs with the class DBStorage, generating an object
that can use the storage methods of all the defined classes.
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
Finally, the same way we aggregated properties and methods
dynamically, we can also deaggregate them from the object.
So, if we deaggregate the class WDDXStorage from $fs, we
will obtain:
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
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 FileStorage
defines a $data property, and the class WDDXStorage also
defines a similar property which will not override the one
in the object acquired during instantiation from the class
FileStorage.
Table of Contents
aggregate_info -- Returns an associative array of the methods
and properties from each class that has been aggregated
to the object.
aggregate_methods_by_list -- Selective dynamic class methods
aggregation to an object
aggregate_methods_by_regexp -- Selective class methods aggregation
to an object using a regular expression
aggregate_methods -- Dynamic class and object aggregation
of methods
aggregate_properties_by_list -- Selective dynamic class
properties aggregation to an object
aggregate_properties_by_regexp -- Selective class properties
aggregation to an object using a regular expression
aggregate_properties -- Dynamic aggregation of class properties
to an object
aggregate -- Dynamic class and object aggregation of methods
and properties
aggregation_info -- Alias of aggregate_info()
deaggregate -- Removes the aggregated methods and properties
from an object