2016-07-04 26 views
-1

3レベルの構造でクラスを継承するものを書いてみたい。そのような例の抽象的な例。PHP - クラスの継承と別のクラスとの組み合わせ

couriers_list - >宅配便 - > courier_name

class couriers_list { 
    // I'm searches courier_name of the database and reference to the class of courier_name found in database 

    public function show_courier() { 
     return $this->xxx(); 
    } 
} 

class courier extends couriers_list { 
    // Function for the courier 
    // Probably it would interface or abstraction 
} 

class courier_name extends courier { 
    // Methods of accurately courier eg. Calculation associated with the delivery 
    // Here each courier would separate file php eg. ups.php 

    public function xxx() { 
     return 'xxx'; 
    } 
} 

$k = new couriers_list(); 
echo $k->xxx(); 

私は、メソッドの子孫へのアクセスを持っていないので、私は、courier_listする宅配便を延長することができるとは思いません。外部からは、couriers_listクラスだけを参照したいと思います。データベースから情報を取得し、courier_nameの結果のメソッドを取得する必要があります。

オブジェクトを整理するにはどうすればいいですか?

+0

アクセスもでき、 '*' couriers_list' *含まれていると思われますcourier'オブジェクトは名前を持っています(おそらく 'string'でなければなりません!)。あなたは、名前のような単純なプロパティのために別のクラスを作ることを望まないでしょう。 – Pete

+0

問題を示すコードを編集しました。私は上層階のcouriers_listの外から電話したいと思う。上位クラスからは、下位クラスの操作を実行したいので、下位クラスcourier_nameと呼ばれるようにはわかりません。 –

+0

私は '$ ks = new courier_name(); $ ks-> xxx();を返すことができます。 show_courierメソッドでは、私はそれが良いアイデアだとは知らない。 –

答えて

0

それはあなたのクーリエは、それぞれのその後

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 
関連する問題