将PHP Class Private值设置为变量

时间:2017-05-11 18:03:27

标签: php pdo pagination

我正在尝试编辑PHP类以对查询结果进行分页。使用此代码

设置每页返回的记录数
private $_limit_per_page = 20;

可以看出,这设置为20,但我想在每个使用变量$ paginate的代码页面上设置它。

$paginate = 20;

然后在Paginator类中

private $_limit_per_page = $paginate;

我试图将代码放在另一个类中,但由于我对编码要求不是很熟悉,所以我有点挣扎,所以任何帮助都会非常感激。

菲尔

1 个答案:

答案 0 :(得分:0)

请尝试以下方法:

class Pag { //create pagination class
  private $_limit_per_page;  //set private variable

  function __construct($limit = 20){  //set contructor to populate the limit to 20 by default, but override if something is passed
    $this->_limit_per_page = $limit;  //set the value to whatever was passed
  }
  function showMe() { //create a function that can output the variable, so you can see if it worked
    echo $this->_limit_per_page;  //output the variable
  }
}
$paginate = new Pag(35);  //instantiate the class, but override the limit to 35

echo $paginate->showMe(); //run it