警告 |
このモジュールは、 実験的なものです。これは、これらの関数の動作、関 数名は、このドキュメントに書かれて事項と同様に告知なく将来的なPHPのリ リースで変更される可能性があります。注意を喚起するとともに、このモジュー ルは使用者のリスクで使用してください。 |
オブジェクト指向プログラミングでは、簡単なクラス(またはインスタン ス)を組み合わせてより複雑なクラスを作成するということが一般に行わ れます。これは、複雑なオブジェクトやオブジェクト階層を構築するた めの柔軟な方法であり、多重継承と同等のことを動的に行う機能を有し ます。 クラス(またはオブジェクト)を合成するには、合成される要素の間の関 係により関連(Association)と 集約(Aggregation)の2種類の方法があります。
関連は、独立に構築され、外部から 可視の部分を合成したものです。クラスまたはオブジェクトを関連づけ る際、各クラスは、関連するクラスへのリファレンスを保持します。 複数のクラスを静的に関連づける際、クラスは他のクラスのインスタン スへのリファレンスを含みます。例えば、
例 2. オブジェクトの関連
|
一方、集約では、合成されたパーツのカプセル化 (隠蔽)が行われます。(静的な)内部クラス(PHPはまだ内部クラスをサポー トしていません)を使用することにより、クラスを集約することができま す。この場合、このクラスを含むクラスを通じる場合以外、集約された クラスの定義にはアクセスできません。複数のインスタンスの集約(オブ ジェクト集約)は、あるオブジェクトの内部にサブオブジェクトを動的に 作成することを意味し、この過程でこのオブジェクトのプロパティとメ ソッドを拡張します。
オブジェクトの集約は、(例えば、分子は原子を集約したものであるといっ た)包含関係を表す際の自然な方法であり、サブクラスを複数の親クラス およびそのインターフェイスに永続的にバインドすることなく、 多重継承と等価な機能を得るために使用できます。 実際、オブジェクトの集約はより柔軟に使用することができ、集約され るオブジェクトで継承するメソッドまたはプロパティを選択することが できます。
3つのクラスを定義し、各々に別々のストレージメソッドを実装します。
例 3. storage_classes.inc
|
We then instantiate a couple of objects from the defined classes, and perform some aggregations and deaggregations, printing some object information along the way:
例 4. test_aggregation.php
|
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.