2011-08-02 9 views
0

私はOOPを初めて使っています。私は拡張されたSiteという名前のこのクラスを作成しましたが、一般的なクエリーとエキサイティングを持つ他の多くのクラスも作成しました。 この特定のクラス "ページネーション"では、他のインスタンスからアクセスされるメソッドと、内部的に送信されるデータへのアクセスが必要です。それはおそらく書かれた悪いですが、私はいくつかの助けが必要です。php5オブジェクトから別のクラスにアクセスする

<?php 

    class Site { 
     public $lang; 
     public $user_agent; 
     public $user_lang; 
     public $url; 
     public $bots; 

     public $total_items; 
     public $itemsPerPage; 
     public $page; 

class Products extends Site {  
      function getProducts($last = false, $id = false, $cat = false, $active_only = false, $lang_pref = false, $itemsPerPage = false, $page =false){ 

      //code 

      //prepare paging 
      if(!empty($itemsPerPage)){ 

       //count total product 
       $count = mysql_query(
       "SELECT COUNT(*) 
       FROM products_list 
       "[email protected]$cat_to_choose." 
       "[email protected]$lang." 
       ") 
       or die(mysql_error()); 

       $count = mysql_fetch_row($count); 
       $total_items = $count[0]; 

       // set vars for Pagination class 
       $this->total_items = $total_items; 
       $this->itemsPerPage = $itemsPerPage; 
       $this->page = mysql_real_escape_string($page); 

       //send data to class Pagination 
       $pagination = new Pagination(); 
       $start = $pagination->createPagination(); 

       $limit = "LIMIT ".$start.", ".$itemsPerPage; 

      } 

         //code 
     } 




      //other classes 

    class Pagination extends Site { 


     function createPagination(){ 

      // get total pages by dividing 
      $this->total_pages = $this->total_items/$this->itemsPerPage; 

      //round to highest integer 
      $this->total_pages= ceil($this->total_pages); 

      switch($this->page){ 
       case "0": 
       case null: 
       case "1": 
        $start = 0; 
        break; 
       default : 
        $start = ($this->page-1)*($this->itemsPerPage); 

        break; 

      } 

      return $start; 

     } 

     public function htmlPagination(){ 
      if($this->page == 0){ 
       $this->page = 1; 
      } 
      $pagination = array(
        "total_pages" => $this->total_pages, 
        "current_page" => $this->page, 
        "total_items" => $this->total_items 
      ); 

      return $pagination; 

     } 

    } 
    } 

PHPコード

$products_object= new Products(); 
$products = $products_object->getProducts(false,false,$cat, true, $site->lang, $itemsperpage, @$_GET["pag"]); 

私はこれをしなかったら、どのように私は、製品のインスタンスで処理されたデータでhtmlPaginationにアクセスできますか?

答えて

1

プロダクトオブジェクトのフィールドとしてページ番号を設定し、getメソッドで取得するか、publicとして定義して直接読み取ることができます。製品には

class Products 
{ 
    ... 
    private $pagination; 

    public function getProducts(...) 
    { 
     ... 
     $this->pagination = new Pagination(); 
     ... 
    } 

    public function getPagination() 
    { 
     return $this->pagination; 
    } 
} 

その後、後:

$product->getPagination()->htmlPagination(); 

のhtmlページネーションを取得します。

+0

問題は、他のクラスのページネーションメソッドも使用することです。 –

+0

それぞれがそのようなメソッドを実装できます。 – Dan

関連する問題