2011-08-02 4 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 메소드로 검색하거나 공개로 정의하고 직접 읽을 수 있습니다. 제품에

:

class Products 
{ 
    ... 
    private $pagination; 

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

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

그리고 나중에 :

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

는 HTML 페이지 매김을 검색 할 수 있습니다.

+0

문제는 내가 다른 클래스의 페이지 매김 메소드도 사용한다는 것입니다. –

+0

각각 이러한 메소드를 구현할 수 있습니다. – Dan

관련 문제