在 class public 变量为 NULL
In class public variable is NULL
我有一个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
}
怎么了?
<?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
我有一个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
}
怎么了?
<?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