それはあなたのクーリエは、それぞれのその後
interface CourrierInterface
{
const CLASS_NAME = __CLASS__;
public function getName();
public function getPrices();
public function XXX();
public function calculateDelivery($from, $to);
/** these are just example add here all the method that define a courier */
}
を使用されるべき方法を定義するために使用されるので、あなたのクラスクーリエは、インターフェイスでなければなりませんが、あなたが持っているクーリエ、あなたはデCourrierInterface
class UpsCourrier implements CourrierInterface
{
private $name;
public function getName()
{
return $this->name;
}
public function setName($name)
{
$this->name = $name;
}
public function XXX()
{
// do something
}
}
Iを実装しなければなりませんCourrierListがデータベースについて知っているべきだと思ってはいけませんが、あなたが持っている全てのオブジェクトを含んでいる必要があります。 ここで私は、コレクション(TypedCollectionクラス)を管理するために、プロジェクト
https://github.com/yvoyer/collection
を使用し、それはあなたのコレクション(ここではcourrierInterface)にしたいクラスの種類を制御することができますが、あなたあなたがそれをもっと簡単にしたいならば、配列を使うことができます。
私は2つのXXXメソッドを作成します.1つは特定のコースター(そのリスト以降)であり、もう1つはすべてのリストに適用されます。私はので、私はCourrierListがデータベースについて知っておくべきとは思わないので、あなたはあなたのDB内のデータをフェッチし、すべての作成ん工場を作成することができますが、
class CourrierList
{
/**
* TypedCollection
*/
private $courrierList;
public function __construct()
{
// i prefer to use a collection class that valid my type instead of an array, your choice!
$this->courrierList = new TypedCollection("CourrierInterface");
}
public function addCourrier(CourrierInterface $courrier)
{
$this->courrierList->add($courrier);
}
public function removeCourrier(CourrierInterface $courrier)
{
$this->courrierList->removeElement($courrier);
}
protected function getCourrierByName($courrierName)
{
$closure = function (CourrierInterface $courrier) use ($courrierName) {
return $courrier->getName() == $courrierName;
};
return $this->courrierList->filter($closure)->toArray();
}
// since its a list, you must be able to select your courrier to execute the XXX() method
public function XXXByName($name)
{
$courrier = $this->getCourrierByName($name);
$courrier->XXX();
}
// if you prefer to apply to all your courrierList just do
public function XXX()
{
foreach ($this->courrierList as $courrier) {
$courrier->XXX();
}
}
}
が必要なものを取るあなたの必要性を知りません求刑者。ここではdbConnectionはあなたの選択ですが、pdoなど...私はちょうど例を作った、良い方法を見つけるために見つける。
class CourrierListFactory
{
private $dbConnection;
public function __construct(DbConnection $connection)
{
$this->dbConnection = $connection;
}
public function createCourrierList()
{
$results = $this->dbConnection->query(".....")->getResults();
$courrierList = new CourrierList();
foreach ($results as $result) {
$courrier = null;
switch ($result['name']) {
case 'ups':
$courrier = new UpsCourrier();
break;
case 'fedex':
$courrier = new FedexCourrier();
break;
/** and so on ... */
default:
// maybe throw exception if courier is not handle
}
$courrier->setName("....");
/** prepare your object here */
// add the courrier to the list
$courrierList->addCourrier($courrier);
}
return $courrierList;
}
}
最後にそのあなたは、まずあなたのリストを構築、のDBConnectionを作成し、このすべてを使用する方法、そして、あなたが私に
$dbConnection = new DbConnection();
// the factory allows you to separate the database from the List class and may be to generate
// your list in an other way later (using apis, etc...)
$factory = new CourrierListFactory($dbConnection);
$courrierList = $factory->createCourrierList();
$courrierList->XXX(); // apply to all
$courrierList->XXXByName("ups"); // apply just to ups courrier
アクセスもでき、 '*' couriers_list' *含まれていると思われますcourier'オブジェクトは名前を持っています(おそらく 'string'でなければなりません!)。あなたは、名前のような単純なプロパティのために別のクラスを作ることを望まないでしょう。 – Pete
問題を示すコードを編集しました。私は上層階のcouriers_listの外から電話したいと思う。上位クラスからは、下位クラスの操作を実行したいので、下位クラスcourier_nameと呼ばれるようにはわかりません。 –
私は '$ ks = new courier_name(); $ ks-> xxx();を返すことができます。 show_courierメソッドでは、私はそれが良いアイデアだとは知らない。 –