php5从对象访问另一个类
我是新手。我创建了这个名为Site的类,这个类是扩展的,但还有许多其他类具有常见的查询和执行。 对于这个特殊的类“分页”,我需要从其他实例访问它的方法,并访问“内部”发送给它的数据。可能写得不好,但我需要一些帮助php5从对象访问另一个类,php,oop,class,Php,Oop,Class,我是新手。我创建了这个名为Site的类,这个类是扩展的,但还有许多其他类具有常见的查询和执行。 对于这个特殊的类“分页”,我需要从其他实例访问它的方法,并访问“内部”发送给它的数据。可能写得不好,但我需要一些帮助 <?php class Site { public $lang; public $user_agent; public $user_lang; public $url; public $bo
<?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
".@$cat_to_choose."
".@$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;
}
}
}
完成此操作后,如何使用Products实例中处理的数据访问
htmlPagination
。您可以将分页设置为Products对象的字段,并使用get方法检索它,或者将其定义为public并直接读取
在产品方面:
class Products
{
...
private $pagination;
public function getProducts(...)
{
...
$this->pagination = new Pagination();
...
}
public function getPagination()
{
return $this->pagination;
}
}
随后:
$product->getPagination()->htmlPagination();
检索html分页。问题是我对其他类使用分页方法,因为它们中的每一个都可以实现这样的方法。
$product->getPagination()->htmlPagination();