あなたが与えた例は多対多の関連付けであるため、3d オブジェクトを使用して関連付けを表す方が適切です。進行中の場合(1対多)、あなたが述べたように単純な構成で問題ありません。
以下はサンプル コードです。コードで行うことができるいくつかの改善点 1. ツールとテクノロジのゲッター セッターを実装し、変数を非公開にします。2. Tools & Technology のクラスではなくインターフェイスを直接使用します。3. ペア クラスで 2 つの異なるインデックス (配列) を使用して、get 関数のパフォーマンスを向上させました。パフォーマンスが問題にならない場合は、1 つの配列を使用できます。
<?php
class Technology{
public $id;
public $title;
public function __construct($id, $title){
$this->id = $id;
$this->title = $title;
}
}
class Tool{
public $id;
public $title;
public function __construct($id, $title){
$this->id = $id;
$this->title = $title;
}
}
class TechnologyToolPair{
private $techIndex = array();
private $toolIndex = array();
//Index by id, you can replace title if u maily search by title
public function addPair($tech, $tool){
$this->techIndex[$tech->id]['technology'] = $tech;
$this->techIndex[$tech->id]['tool'][] = $tool;
$this->toolIndex[$tool->id]['tool'] = $tool;
$this->toolIndex[$tool->id]['technology'][] = $tech;
}
public function getByTechnologyId($id){
return $this->techIndex[$id];
}
public function getByToolId($id){
return $this->toolIndex[$id];
}
public function getByTechnologyName($name){
foreach($this->techIndex as $index => $value){
if(!strcmp($name, $value['technology']->title)){
return $value;
}
}
}
}
$tech1 = new Technology(1, 'php');
$tech2 = new Technology(2, 'java');
$tool1 = new Tool(1, 'eclipse');
$tool2 = new Tool(2, 'apache');
$tool3 = new Tool(3, 'tomcat');
$pair = new TechnologyToolPair();
$pair->addPair($tech1, $tool1);
$pair->addPair($tech1, $tool2);
$pair->addPair($tech2, $tool1);
$pair->addPair($tech2, $tool3);
var_dump($pair->getByToolId(1));
var_dump($pair->getByTechnologyId(2));
var_dump($pair->getByTechnologyName('java'));