我有一个Class,我想在函数中设置$ offset变量,并获得另一个函数
public $db, $_offset, $_limit;
public function Pagination($offset,$limit)
{
$limit = 2;
$page = isset($_GET['page']) ? abs((int)$_GET['page']) : 1;
if ($page <= 0)
{
$page = 1;
}
$offset = $page;
$this->_offset = $offset; // set value for this->_offset
$this->_limit = $limit; // set value for this->_limit
return true;
}
public function SearchAd($value)
{
global $offset,$limit;
var_dump( $this->_limit); // this will return NULL
var_dump( $this->_offset); // this will return NULL
}
有什么问题?
答案 0 :(得分:0)
<?php
class Paginator {
public $db, $_offset, $_limit;
public function __construct($offset, $limit = 20){
$offset = isset($_GET['page']) ? abs((int)$_GET['page']) : 1;
if ($offset <= 0)
{
$offset = 1;
}
$this->_offset = $offset; // set value for this->_offset
$this->_limit = $limit; // set value for this->_limit
return true;
}
public function SearchAd($value)
{
var_dump( $this->_offset); // this will return NULL
var_dump( $this->_limit); // this will return NULL
}
}
$pagination = new Paginator(5,10);
$pagination->SearchAd("asd");
结果: 1 10