このように 1 回ですべてを行うことはできませんarray()。このようにツリーを設定できますが、複数の親やその他の関係を持つより複雑なグラフを設定するには、複数行のコードが必要です。
これにOOを投げると、大いに役立ちます。Person関係を管理するのに役立つクラスを作成しましょう。基本的に、私たちは人々と他の人々との関係を持っているので、そこから始めます.
人物クラス
私が想像するのは、一人一人がさまざまな関係を持っているということです。この配列は、最初に関係のタイプ (「親」や「子」など) によって索引付けされます。各エントリは の配列になりますPerson。
class Person {
var $name, $relations;
function __construct($name) {
$this->name = $name;
$this->relations = array();
}
function addRelation($type, $person) {
if (!isset($this->relations[$type])) {
$this->relations[$type] = array();
}
$this->relations[$type][] = $person;
}
// Looks up multiple relations, for example "parents".
function getRelations($type) {
if (!isset($this->relations[$type])) {
return array();
}
return $this->relations[$type];
}
// Looks up a single relation, for example "spouse".
function getRelation($type) {
$relations = $this->getRelations($type);
return empty($relations) ? null : $relations[0];
}
function __toString() {
return $this->name;
}
フレンドリーな加算器とゲッター
上記を基礎として、よりわかりやすい名前のメソッドをいくつか追加できます。説明のために、親子関係と配偶者を扱います。
function addParents($mom, $dad) {
$mom->addChild($this);
$dad->addChild($this);
}
function addChild($child) {
$this ->addRelation('children', $child);
$child->addRelation('parents', $this);
}
function addSpouse($spouse) {
$this ->addRelation('spouse', $spouse);
$spouse->addRelation('spouse', $this);
}
function getParents () { return $this->getRelations('parents'); }
function getChildren() { return $this->getRelations('children'); }
function getSpouse () { return $this->getRelation ('spouse'); }
}
人づくり
これで、何人かの人を作成し、関係をセットアップできます。ビリーと彼の両親のジョンとジェーンを試してみましょう。
$john = new Person('John');
$jane = new Person('Jane');
$billy = new Person('Billy');
$john ->addSpouse ($jane);
$billy->addParents($jane, $john);
そして、次のようにそれらの関係を確認できます。
echo "John is married to " . $john->getSpouse() . ".\n";
echo "Billy's parents are " . implode(" and ", $billy->getParents()) . ".\n";
出力:
ジョンはジェーンと結婚しています。
ビリーの両親はジェーンとジョンです。
家系図を表示する
グラフが大きくなった場合、グラフを再帰的にトラバースできます。基本的な家系図を表示するツリー ウォーク関数の例を次に示します。サラ、夫のマイク、息子のボビーをミックスに追加しました.
$john = new Person('John');
$jane = new Person('Jane');
$sara = new Person('Sara');
$mike = new Person('Mike');
$bobby = new Person('Bobby');
$billy = new Person('Billy');
$john ->addSpouse ($jane);
$sara ->addParents($jane, $john);
$sara ->addSpouse ($mike);
$bobby->addParents($sara, $mike);
$billy->addParents($jane, $john);
function displayFamilyTree($root, $prefix = "") {
$parents = array($root);
if ($root->getSpouse() != null) {
$parents[] = $root->getSpouse();
}
echo $prefix . implode(" & ", $parents) . "\n";
foreach ($root->getChildren() as $child) {
displayFamilyTree($child, "....$prefix");
}
}
displayFamilyTree($john);
出力:
ジョンとジェーン
……サラとマイク
……ボビー
……ビリー
編集:読みやすくするために再現した@Wrikkenのコメントを以下に示します。
それについては確かに。IMHOは、すべての関係に開始日を追加します(終了しない場合はNULLの可能性があります)。養子縁組などと同様に、離婚も発生します。また、逆のタイプと「ping-back」をaddRelation()関数に追加します。
function addRelation($type, $person, $reverseType, $pingback = false) {
if (!isset($this->relations[$type])) {
$this->relations[$type] = array();
}
if (!in_array($person, $this->relations[$type], true)) {
$this->relations[$type][] = $person;
}
if (!$pingback) {
$person->addRelation($reverseType, $this, $type, true);
}
}